Skip to content

Instantly share code, notes, and snippets.

@JmeHsieh
Created March 8, 2012 17:25
Show Gist options
  • Select an option

  • Save JmeHsieh/2002207 to your computer and use it in GitHub Desktop.

Select an option

Save JmeHsieh/2002207 to your computer and use it in GitHub Desktop.
Decode UIImage in background
__weak Photo *p = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
CGImageRef imageRef = image.CGImage;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,
CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef),
8,
CGImageGetWidth(imageRef) * 4,
colorSpace,
kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
if (!context)
return;
CGRect rect = (CGRect){CGPointZero, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)};
CGContextDrawImage(context, rect, imageRef);
CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *decompressedImage = [[UIImage alloc] initWithCGImage:decompressedImageRef];
CGImageRelease(decompressedImageRef);
dispatch_async(dispatch_get_main_queue(), ^{p.thumbImage = decompressedImage;});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment