Search code examples
ioscropcgimagecore-image

CoreImage CICrop returning 0x0 pixel image


Apologies for so many questions today, Friday dead brain symptom setting in.

I have a CICrop filter:

[crop setValue:beginImage forKey:@"inputImage"];  //give the crop filter the beginImage
    [crop setValue:[CIVector vectorWithX:0.0f Y:0.0f Z:_exportSize W:_exportSize] forKey:@"inputRectangle"];  //give the filter the size to crop to
    croppedImage = [crop valueForKey:@"outputImage"];  //set the output image, note it's still a CIImage at this point

Sadly, it's not working. The image it crops ends up being 0x0 pixels. A quick NSLog of _exportSize (which is a CGFloat) reveals it's 1024, but the log shows that the image post crop is 0x0. I cannot work out why.

This code worked before. The only difference being, I think, is that I used to crop UIImage no CIImages. I'd rather not have to convert as I plan on doing other CoreImage stuff after the crop.

As I say, _exportSize is a CGFloat _exportSize; and logging it reveals it 1024.0000.

Any ideas?

Cheers.


Solution

  • In the end I gave up with Core Image for cropping and used this instead:

        -(UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
        CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
    
        UIImage *cropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
    
        return cropped;
    }