azamsharp
azamsharp

Reputation: 20086

Displaying UIImage obtained from CIImage in iOS 5 Core Image Filters

I am working on image filter app. I have created the filter using the code below:

context = [CIContext contextWithOptions:nil];



    beginImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"biscus_small.png"]];


    filter = [CIFilter filterWithName:@"CISepiaTone" 
                                  keysAndValues: kCIInputImageKey, beginImage, 
                        @"inputIntensity", [NSNumber numberWithFloat:0.8], nil];

Then when the user selects the filter I simply get the CIImage after the filter has applied and assign to the UIImageView. For some reason the UIImageView after setImage displays white (nothing).

CIImage *outputImage = [filter outputImage];

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

    UIImage *finalImage = [UIImage imageWithCIImage:outputImage];

    [self.imageView setImage:finalImage];

If I use createCGImage method of the CIContext then it works fine and displays the image with filter but using createCGImage is tremendously slow.

Upvotes: 1

Views: 3198

Answers (1)

Brad Larson
Brad Larson

Reputation: 170317

The reason why CIImage's -createCGImage:fromRect: is so slow is that this is what actually triggers the processing and output of the raster bitmap. Assigning the output CIImage to a UIImage does not cause the image to be processed and output rendered. You won't be able to avoid this step when filtering an image via Core Image.

As I've stated in this answer, Core Image's performance on iOS is a little disappointing as of iOS 5.1. If I may make a suggestion, I've built an open source framework that does GPU-accelerated image processing and beats the performance of Core Image under almost all circumstances I've tested. For image filtering I'm about 4 times faster than Core Image on an iPhone 4.

However, there still is some overhead in going from and to a UIImage when filtering, so if you have the ability to work with the raw image bytes, my raw data input and output is much faster than dealing with UIImages. I'm going to add some direct JPEG and PNG input and output in this framework to provide a faster path for this than having to go through UIImage.

Upvotes: 6

Related Questions