Created
August 13, 2011 20:52
-
-
Save steipete/1144242 to your computer and use it in GitHub Desktop.
Preload UIImage for super-smooth interaction. especially great if you use JPGs, which otherwise produce a noticeable lag on the main thread.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UIImage *)pspdf_preloadedImage { | |
CGImageRef image = self.CGImage; | |
// make a bitmap context of a suitable size to draw to, forcing decode | |
size_t width = CGImageGetWidth(image); | |
size_t height = CGImageGetHeight(image); | |
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB(); | |
CGContextRef imageContext = CGBitmapContextCreate(NULL, width, height, 8, width*4, colourSpace, | |
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little); | |
CGColorSpaceRelease(colourSpace); | |
// draw the image to the context, release it | |
CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image); | |
// now get an image ref from the context | |
CGImageRef outputImage = CGBitmapContextCreateImage(imageContext); | |
UIImage *cachedImage = [UIImage imageWithCGImage:outputImage]; | |
// clean up | |
CGImageRelease(outputImage); | |
CGContextRelease(imageContext); | |
return cachedImage; | |
} |
Pls don't forget to add old image orientation with:
let cachedImage = UIImage(cgImage: outputImage, scale: scale, orientation: imageOrientation)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the snippet!
Swift 3 version: