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);
}
The method is called within:
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
- 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.
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.
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.
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)
EDIT: Just made an experiment:
NSLog(@"rawImage: %@ - rawImage.CGImage: %@", rawImage, rawImage.CGImage);
NSLog(@"endImage: %@ - endImage.CGImage: %@", endImage, endImage.CGImage);
It looks like only the rawImage
(coming from the UIImagePickerController
) possesses a backing CGImageRef
. The other one, created from a CIImage
, doesn't.
Can it be that UIImageWriteToSavedPhotosAlbum
works using the backing CGImageRef
?