the doors - building a performant media viewer with gcd

8
THE DOORS Building a performant media viewer with GCD

Upload: open-input

Post on 21-Jun-2015

176 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: The Doors - Building a performant media viewer with GCD

THE DOORSBuilding a performant media viewer with GCD

Page 2: The Doors - Building a performant media viewer with GCD

THE DOORSKnowing the app

Page 3: The Doors - Building a performant media viewer with GCD

THE DOORSThe problem

Page 4: The Doors - Building a performant media viewer with GCD

THE DOORSStanding on the shoulders of giants

GMGridViewhttps://github.com/gmoledina/GMGridView

iCarouselhttps://github.com/nicklockwood/iCarousel

UIImage categorieshttps://github.com/jchatard/UIImage-categories

Page 5: The Doors - Building a performant media viewer with GCD

THE DOORSThe newbie solution

- (void)setImageURL:(NSURL *)imageURL{ self.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];}

- (void)prepareForReuse{ self.imageView.image = nil;}

Page 6: The Doors - Building a performant media viewer with GCD

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;}

Page 7: The Doors - Building a performant media viewer with GCD

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;}