1

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 raw UIImage.
  • 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?

2 Answers 2

5

Can it be that UIImageWriteToSavedPhotosAlbum works using the backing CGImageRef?

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.

3
  • Thanks. Actually this is just a preliminary test. I'll be needing CIFilters later. I know there is a filter to crop CIImages, and maybe I'll use that directly. Anyway, could you please provide a plain example of cropping by drawing into a smaller graphics context? Also, how would I use a CIContext to turn the CIImage back into a bitmap? Cheers.
    – tompave
    May 6, 2013 at 14:54
  • Cropping by drawing into a smaller context: apeth.com/iOSBook/ch15.html#_uiimage_drawing
    – matt
    May 6, 2013 at 14:58
  • What CIImage is for: apeth.com/iOSBook/ch15.html#_cifilter_and_ciimage
    – matt
    May 6, 2013 at 14:59
2

If the UIImage object was initialized using a CIImage object, the value of the property is NULL.

You can generate UIImage from CIImage like this:

let lecturePicture = UIImage(data: NSData(contentsOfURL: NSURL(string:"http://i.stack.imgur.com/Xs4RX.jpg")!)!)!
let controlsFilter = CIFilter(name: "CIColorControls")
controlsFilter.setValue(CIImage(image: lecturePicture), forKey: kCIInputImageKey)
controlsFilter.setValue(1.5, forKey: kCIInputContrastKey)
let displayImage = UIImage(CGImage: CIContext(options:nil).createCGImage(controlsFilter.outputImage, fromRect:controlsFilter.outputImage.extent()))!
displayImage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.