Last active
May 10, 2023 17:50
-
-
Save leviathan/7688467 to your computer and use it in GitHub Desktop.
UIImage offscreen rendering
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
// 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