UIImageWriteToSavedPhotosAlbum()不保存裁剪的图像 [英] UIImageWriteToSavedPhotosAlbum() doesn't save cropped image
问题描述
我正在尝试将裁剪的图像保存到相机胶卷。
(我需要以编程方式进行,我无法让用户编辑它)
I'm trying to save a cropped image to the camera roll.
(I need to do it programmatically, I can't have the user edit it)
这是我的(仍然非常基本的)剪切和保存代码:
This is my (still quite basic) cut and save code:
- (void)cutAndSaveImage:(UIImage*)rawImage
{
CIImage *workingImage = [[CIImage alloc] initWithImage:rawImage];
CGRect croppingRect = CGRectMake(0.0f, 0.0f, 3264.0f, 1224.0f);
CIImage *croppedImage = [workingImage imageByCroppingToRect:croppingRect];
UIImage *endImage = [UIImage imageWithCIImage:croppedImage scale: 1.0f orientation:UIImageOrientationRight];
self.testImage.image = endImage;
UIImageWriteToSavedPhotosAlbum(rawImage, self, @selector(image:didFinishSavingWithError:contextInfo:) , nil);
UIImageWriteToSavedPhotosAlbum(endImage, self, @selector(image:didFinishSavingWithError:contextInfo:) , nil);
}
该方法在以下范围内调用:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
The method is called within:
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
- 我首先使用原始
UIImage
创建CIImage
。 - 然后我使用第一个实例方法获得裁剪
CIImage
。 - 之后我使用裁剪的CIImage创建一个新的
UIImage
。
- I first create a
CIImage
using the rawUIImage
. - Then I get a cropped
CIImage
using an instance method of the first one. - After that I create a new
UIImage
using the cropped CIImage.
此时,为了获得一些反馈,我设置了新裁剪的 UIImage
作为 UIImageView
的支持图像。这是有效的,我可以清楚地看到图像完全按照我想要的方式裁剪。
At this point, to have some feedback, I set the new cropped UIImage
as the backing image of a UIImageView
. This works, and I can clearly see the image cropped exactly how I desired.
然而,当我尝试将其保存到相机胶卷时,事情就会停止工作。
我无法保存新创建的 endImage
。
如你所见,我添加了一行来保存原来的 UIImage
,仅供参考比较。原始版本正常保存。
When I try to save it to the camera roll, however, things stop working.
I can't save the newly created endImage
.
As you can see, I added a line to save the original UIImage
too, just for comparison. The original one saves normally.
另一个令人困惑的事情是 NSError
对象传递给 image:didFinishSavingWithError:contextInfo:
回调为零。 (通常会对两次保存尝试执行回调)
Another confusing thing is that the NSError
object passed to the image:didFinishSavingWithError:contextInfo:
callback is nil. (the callback is normally executed for both saving attempts)
编辑:
刚做了一个实验:
Just made an experiment:
NSLog(@"rawImage: %@ - rawImage.CGImage: %@", rawImage, rawImage.CGImage);
NSLog(@"endImage: %@ - endImage.CGImage: %@", endImage, endImage.CGImage);
看起来只有 rawImage
(即将推出)来自 UIImagePickerController
)拥有支持 CGImageRef
。另一个是从 CIImage
创建的,但却没有。
It looks like only the rawImage
(coming from the UIImagePickerController
) possesses a backing CGImageRef
. The other one, created from a CIImage
, doesn't.
是否可以 UIImageWriteToSavedPhotosAlbum
使用支持 CGImageRef
?
推荐答案
UIImageWriteToSavedPhotosAlbum可以使用支持CGImageRef工作吗?
Can it be that UIImageWriteToSavedPhotosAlbum works using the backing CGImageRef?
正确。 CIImage不是图像,只有CIImage支持的UIImage也不是图像;它只是一种包装。你为什么一直在使用CIImage?您没有使用CIFilter,所以这没有任何意义。或者,如果 使用CIFilter,则必须通过CIContext渲染才能将输出作为位图。
Correct. A CIImage is not an image, and a UIImage backed only by a CIImage is not an image either; it is just a kind of wrapper. Why are you using CIImage at all here? You aren't using CIFilter so this makes no sense. Or if you are using CIFilter, you must render through a CIContext to get the output as a bitmap.
您可以通过绘图轻松裁剪较小的图形上下文。
You can crop easily by drawing into a smaller graphics context.
这篇关于UIImageWriteToSavedPhotosAlbum()不保存裁剪的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!