This whole topic was turned upside down with the release of iOS 5. Apple finally added Core image filters in iOS 5.
Core Image (CI) filters use OpenGL shader code under the covers to do transformations on images
EXTREMELY fast. Many of the filters are fast enough to operate on a video from the camera and display onto the screen in real time.
I haven't tackled using CI filters on video yet, but it's fairly straightforward to use a filter on a still image.
One gotcha is that CI filters take an object called a CIImage as input, and return a CIImage as output.
There's a UIImage method in iOS 5, initWithCIImage, that's supposed to take a CIImage as input and create a UIImage. It doesn't work. it returns a UIImage that has the correct bounds, but lacks the backing CGImage containing the pixels. I wasted a bunch of time yesterday tracking down this bug, and have submitted a radar bug. Please, everybody reading this, go write your own variation on this bug. Apple uses the number of bug reports they get to set their priorities on bug fixes.
Below is a sample method I wrote yesterday that takes a UIImage as input, and returns an adjusted UIImage as output. It uses the CIColorControls method to adjust the brightness of an image. It would be trivial to change it to use lots of different Core Image filters.
The "if (TRUE)" bit enables the work-around code that makes it possible to convert a CIIImage to a CGImage, and then to a UIImage. If you change "if (TRUE)" to "if (FALSE)", the code tries to use the Apple iOS 5 method, which doesn't work.
I got the work-around from Erica Sadun's new "iOS 5 Developer's Cookbook" from Addison Wesley. (
http://www.amazon.com/iOS-Developers...6983449&sr=8-1)
This book is truly outstanding. It is chock-full of useful information, and includes a decent introduction to Objective C, Xcode, the portal, etc. Where it really shines is tons of working examples of techniques you can use to solve real-world problems in your applications.
Erica is has a PHD in computer science, and her knowledge of iOS is both deep and wide. Plus, she writes well.
I think it's the best fit for intermediate to advanced programmers. If you've got experience with development on another platform, and are learning iOS, it should be perfect.
I can't say enough good things about this book.
Unfortunately, there was a problem with the first printing, and it had to be scrapped. The new version is due out in 10 days. I have one of the misprints, and even with the printing problems, I would gladly pay twice the price for it.
Code:
- (UIImage *) changeImageBrightness: (UIImage *) anImage
amount: (CGFloat) amount;
{
UIImage *adjustedUIImage;
CIImage *adjustedCIImage;
CGImageRef adjustedCGImage;
CGImageRef inputCGImage = anImage.CGImage;
CIImage *inputCIImage = [CIImage imageWithCGImage: inputCGImage];
CIFilter *colorFilter = [CIFilter filterWithName: @"CIColorControls"
keysAndValues:
@"inputBrightness", [NSNumber numberWithFloat: amount],
@"inputImage", inputCIImage,
nil];
adjustedCIImage = [colorFilter valueForKey: kCIOutputImageKey];
if (TRUE)
{
//This works
adjustedCGImage = [[CIContext contextWithOptions:nil] createCGImage: adjustedCIImage fromRect:inputCIImage.extent];
adjustedUIImage = [UIImage imageWithCGImage: adjustedCGImage];
}
else
{
//This doesn't work
adjustedUIImage = [UIImage imageWithCIImage: adjustedCIImage];
adjustedCGImage = adjustedUIImage.CGImage;
}
return adjustedUIImage;
}