Skip to content

Instantly share code, notes, and snippets.

@douglashill
Forked from tonymillion/gist:6716935
Last active March 5, 2021 06:20
Show Gist options
  • Save douglashill/2f8f18aab8ebcf79a996 to your computer and use it in GitHub Desktop.
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.
@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