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; | |
} |
Any idea whether this applies to watchOS as well?
ah... why apple don't let us:
let image = UIImage(...)
image.prepareForRenderingAsynchronously = true
Thanks for the snippet!
Swift 3 version:
import UIKit
extension UIImage {
func forceLoad() -> UIImage {
guard let imageRef = self.cgImage else {
return self //failed
}
let width = imageRef.width
let height = imageRef.height
let colourSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo: UInt32 = CGImageAlphaInfo.premultipliedFirst.rawValue | CGBitmapInfo.byteOrder32Little.rawValue
guard let imageContext = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width * 4, space: colourSpace, bitmapInfo: bitmapInfo) else {
return self //failed
}
let rect = CGRect(x: 0, y: 0, width: width, height: height)
imageContext.draw(imageRef, in: rect)
if let outputImage = imageContext.makeImage() {
let cachedImage = UIImage(cgImage: outputImage)
return cachedImage
}
return self //failed
}
}
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
@rasaunders100: Using
kCGImageAlphaNoneSkipFirst
instead ofkCGImageAlphaPremultipliedFirst
gets you an image without alpha.@steipete: I have seen a similar snippet floating around which does a few additional checks, such as:
What do you think about these – are they unnecessary?