Date post: | 21-Jun-2015 |
Category: |
Technology |
Upload: | open-input |
View: | 176 times |
Download: | 3 times |
THE DOORSBuilding a performant media viewer with GCD
THE DOORSKnowing the app
THE DOORSThe problem
THE DOORSStanding on the shoulders of giants
GMGridViewhttps://github.com/gmoledina/GMGridView
iCarouselhttps://github.com/nicklockwood/iCarousel
UIImage categorieshttps://github.com/jchatard/UIImage-categories
THE DOORSThe newbie solution
- (void)setImageURL:(NSURL *)imageURL{ self.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];}
- (void)prepareForReuse{ self.imageView.image = nil;}
THE DOORSThe naive solution
- (void)setImageURL:(NSURL *)imageURL{ [self.activityIndicatorView startAnimating]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ UIImage *originalImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]]; UIImage *resizedImage = [originalImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:self.imageView.bounds.size interpolationQuality:kCGInterpolationHigh]; dispatch_async(dispatch_get_main_queue(), ^{ self.imageView.image = resizedImage; [self.activityIndicatorView stopAnimating]; }); });}
- (void)prepareForReuse{ self.imageView.image = nil;}
THE DOORSThe final(?) solution
- (void)setImageURL:(NSURL *)imageURL{ self.currentImageURL = imageURL; [self.activityIndicatorView startAnimating]; dispatch_async([TDPhotoViewCell imageRenderingDispatchQueue], ^{ @autoreleasepool { // Cells can be reused, so we check to see if it still makes sense to render the image at imageURL if ([self.currentImageURL isEqual:imageURL]) { UIImage *originalImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]]; UIImage *resizedImage = [originalImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:self.imageView.bounds.size interpolationQuality:kCGInterpolationHigh]; dispatch_async(dispatch_get_main_queue(), ^{ if ([self.currentImageURL isEqual:imageURL]) { self.imageView.image = resizedImage; [self.activityIndicatorView stopAnimating]; } }); } } });}
- (void)prepareForReuse{ self.currentImageURL = nil; self.imageView.image = nil;}
+ (dispatch_queue_t)imageRenderingDispatchQueue{ static dispatch_queue_t imageRenderingDispatchQueue; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ imageRenderingDispatchQueue = dispatch_queue_create("TDPhotoViewCell.imageRendering", 0); }); return imageRenderingDispatchQueue;}