Sambit Prakash
Sambit Prakash

Reputation: 341

Unable to get Image Data from CIImage

I am using CIImage and CIFilter to doing image filtration. After that, I am trying to get image data by UIImageJPEGRepresentation, but I am getting null data.

CIImage *beginImage = [[CIImage alloc] initWithImage:sourceImage];

CIContext *context = [CIContext contextWithOptions:nil];

CIFilter *filter = [CIFilter filterWithName:effect keysAndValues:kCIInputImageKey, beginImage, nil];

CIImage *outputImage = [filter outputImage];

CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];

UIImage *filteredImage = [UIImage imageWithCIImage:outputImage];
CGImageRelease(cgimg);

NSData *imageData = UIImageJPEGRepresentation(filteredImage, 1.0);

I am getting null data when I console imageData. Please check my above code, and let me know where I am doing wrong.

Upvotes: 3

Views: 1671

Answers (2)

jignesh Vadadoriya
jignesh Vadadoriya

Reputation: 3310

Your problem is here you are passing CIImage

UIImage *filteredImage = [UIImage imageWithCIImage:outputImage];

But you need to pass CGImageRef while creating UIImage write this

 UIImage *filteredImage = [[UIImage alloc] initWithCGImage:cgimg];

I hope it will help you

Upvotes: 2

Himanshu Moradiya
Himanshu Moradiya

Reputation: 4815

// create our blurred image try this hope its work for you.

 CIContext *context = [CIContext contextWithOptions:nil];
 CIImage *inputImage = [CIImage imageWithCGImage:theImage.CGImage]; //theImage pass your image
 CIFilter *filter1 = [CIFilter filterWithName:@"CIGaussianBlur"];
 [filter1 setValue:inputImage forKey:kCIInputImageKey];
 CIImage *result = [filter1 valueForKey:kCIOutputImageKey];
 CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
 UIImage *returnImage = [UIImage imageWithCGImage:cgImage]; // returnImage is your blur image
 CGImageRelease(cgImage);

Upvotes: 1

Related Questions