-
-
Save douglashill/2f8f18aab8ebcf79a996 to your computer and use it in GitHub Desktop.
Do not use this to decode an image without blocking the main thread, because it does block the main thread for non-obvious reasons.
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
@import ImageIO; | |
@implementation UIImage (DHImageDecoding) | |
/// Warning: do not use. This looks like it should work, but it blocks the main thread for non-obvious reasons. | |
+ (UIImage *)dh_decodedImageWithData:(NSData *)data | |
{ | |
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); | |
if (source == NULL) { | |
return nil; | |
} | |
CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, 0, (__bridge CFDictionaryRef)@{(id)kCGImageSourceShouldCacheImmediately : (id)kCFBooleanTrue}); | |
CFRelease(source); | |
source = NULL; | |
if (cgImage == NULL) { | |
return nil; | |
} | |
UIImage *const uiImage = [self imageWithCGImage:cgImage]; | |
CGImageRelease(cgImage); | |
cgImage = NULL; | |
return uiImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment