SpaceDog SpaceDog - 20 days ago 8
iOS Question

CIImage size extent zero?

I have create this category to convert

CIImage
to
UIImage
. The
CIImage
s I am using are coming from
CIFilter
s.

-(UIImage *)obtainImageWithRetinaScale:(CGFloat)scale
{
CIContext *context = [CIContext contextWithOptions:nil];

//CGRect frame = CGRectMake(0,0,700,392);
CGImageRef processedCGImage = [context createCGImage:self
fromRect:[self extent]];

UIImage *returnImage = [UIImage imageWithCGImage:processedCGImage
scale:scale
orientation:UIImageOrientationUp];
CGImageRelease(processedCGImage);

return returnImage;
}


When this runs,
[self extent]
gives a rect with garbled numbers, that is barely (0, 0, infinity, infinity) ... really huge numbers there.

Obviously processed image is nil.

When I force a frame with the correct size, the image is correctly produced.

HAS HAS
Answer

Use the following code, credits go completely to William Vasconcelos in his answer here which is why I made this a community wiki:

//assume beginImage is CIImage you want to tint            
CIImage* outputImage = nil;

//create some blue
CIFilter* blueGenerator = [CIFilter filterWithName:@"CIConstantColorGenerator"];
CIColor* blue = [CIColor colorWithString:@"0.1 0.5 0.8 1.0"];
[blueGenerator setValue:blue forKey:@"inputColor"];
CIImage* blueImage = [blueGenerator valueForKey:@"outputImage"];

//apply a multiply filter
CIFilter* filterm = [CIFilter filterWithName:@"CIMultiplyCompositing"];
[filterm setValue:blueImage forKey:@"inputImage"];
[filterm setValue:beginImage forKey:@"inputBackgroundImage"];
outputImage = [filterm valueForKey:@"outputImage"];

To bring in some personal contribution and make it worth an answer, here is the Swift 3 version:

// assume beginImage is the CIImage you want to tint
let outputImage: CIImage?
let blueGenerator = CIFilter(name: "CIConstantColorGenerator", withInputParameters: ["inputColor": CIColor(string: "0.1 0.5 0.8 1.0")])
let blueImage = blueGenerator!.outputImage

let filterM = CIFilter(name: "CIMultiplyCompositing", withInputParameters: ["inputImage": blueImage, "inputBackgroundImage": beginImage])
outputImage = filterM.outputImage