我正在使用 CIImage 的 ImageByCroppingToRect 函数来裁剪最初是 UIImage 的图像。庄稼起作用了。当我执行“originalImage.AsJPEG().Save”时也可以。我已经测试并确认它保存的文件是工作 JPEG。但是当我执行“croppedImage.AsJPEG().Save”时,我在其行上收到“System.NullReferenceException:对象引用未设置为对象的实例”错误。originalImage 和croppedImage 都是UIImages,唯一的区别是croppedImage 来自一个转换后的CIImage 进行了裁剪......但是为什么会导致这个错误呢?这是一个 UIImage 所以它应该可以工作,对吧?而且我知道croppedImage 中的图像存在是因为“view.Image = croppedImage;”行
以下是源代码,此链接是包含 jpg 图像的实际项目: https ://www.dropbox.com/s/erh0yrew8pyuye7/TestImageCrop.zip
// Create an image from a file
UIImage originalImage = UIImage.FromFile("IMG_0072.jpg");
// Create a UIImageView
UIImageView view = new UIImageView(new RectangleF(0,0,300,300));
this.View.AddSubview(view);
// Crop the image using CIImage's ImageByCroppingToRect function
CIImage testCIImage = new CIImage(originalImage).ImageByCroppingToRect(new RectangleF(0,0,500,500));
UIImage croppedImage = new UIImage(testCIImage);
view.Image = croppedImage;
// Store the image to a file
string Path = Environment.GetFolderPath ( Environment.SpecialFolder.MyDocuments ) + "/";
NSError err = new NSError();
originalImage.AsJPEG().Save(Path+"originalImage.jpg", true, out err);
// Here is where the error is happening. If I save the originalImage as a JPEG, it works just fine.
// But if I save the croppedImage as a JPEG, it errors out saying "System.NullReferenceException: Object reference not set to an instance of an object"
croppedImage.AsJPEG().Save(Path+"croppedImage.jpg", true, out err);