Skip to content

Instantly share code, notes, and snippets.

@leviathan
Last active May 10, 2023 17:50
Show Gist options
  • Save leviathan/7688467 to your computer and use it in GitHub Desktop.
Save leviathan/7688467 to your computer and use it in GitHub Desktop.
UIImage offscreen rendering
// Define callback blocks
// Success
__block typeof (self) weakSelf = self;
void (^successBlock)(NSURLRequest *, NSHTTPURLResponse *, UIImage *) =
^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
// Assigning an image that has not yet been decoded leads
// to stuttering in UI animations
// Forcing image decoding on a background thread fixes this
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^(void) {
// Force image decoding
UIGraphicsBeginImageContext(image.size);
[image drawAtPoint:CGPointZero];
__block UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Show image view on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:0.3 animations:^{
weakSelf.imageView.image = img;
weakSelf.imageView.alpha = 1.0f;
}];
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment