Saving CGImageRef to a png file?

Using CGImageDestination and passing kUTTypePNG is the correct approach. Here’s a quick snippet: @import MobileCoreServices; // or `@import CoreServices;` on Mac @import ImageIO; BOOL CGImageWriteToFile(CGImageRef image, NSString *path) { CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path]; CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL); if (!destination) { NSLog(@”Failed to create CGImageDestination for %@”, path); return NO; } … Read more

CGImage/UIImage lazily loading on UI thread causes stutter

I’ve had the same stuttering problem, with some help I figured out the proper solution here: Non-lazy image loading in iOS Two important things to mention: Don’t use UIKit methods in a worker-thread. Use CoreGraphics instead. Even if you have a background thread for loading and decompressing images, you’ll still have a little stutter if … Read more

Rotating a CGImage

-(UIImage*) rotate:(UIImage*) src andOrientation:(UIImageOrientation)orientation { UIGraphicsBeginImageContext(src.size); CGContextRef context=(UIGraphicsGetCurrentContext()); if (orientation == UIImageOrientationRight) { CGContextRotateCTM (context, 90/180*M_PI) ; } else if (orientation == UIImageOrientationLeft) { CGContextRotateCTM (context, -90/180*M_PI); } else if (orientation == UIImageOrientationDown) { // NOTHING } else if (orientation == UIImageOrientationUp) { CGContextRotateCTM (context, 90/180*M_PI); } [src drawAtPoint:CGPointMake(0, 0)]; UIImage *img=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; … Read more

Convert image to grayscale

I needed a version that preserved the alpha channel, so I modified the code posted by Dutchie432: @implementation UIImage (grayscale) typedef enum { ALPHA = 0, BLUE = 1, GREEN = 2, RED = 3 } PIXELS; – (UIImage *)convertToGrayscale { CGSize size = [self size]; int width = size.width; int height = size.height; // … Read more