The Doors - Building a performant media viewer with GCD

359 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
359
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

The Doors - Building a performant media viewer with GCD

  1. 1. THE DOORSBuilding a performant media viewer with GCD
  2. 2. THE DOORSKnowing the app
  3. 3. THE DOORSThe problem
  4. 4. THE DOORSStanding on the shoulders of giantsGMGridViewhttps://github.com/gmoledina/GMGridViewiCarouselhttps://github.com/nicklockwood/iCarouselUIImage categorieshttps://github.com/jchatard/UIImage-categories
  5. 5. THE DOORSThe newbie solution- (void)setImageURL:(NSURL *)imageURL{self.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];}- (void)prepareForReuse{self.imageView.image = nil;}
  6. 6. 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:UIViewContentModeScaleAspectFitbounds:self.imageView.bounds.sizeinterpolationQuality:kCGInterpolationHigh];dispatch_async(dispatch_get_main_queue(), ^{self.imageView.image = resizedImage;[self.activityIndicatorView stopAnimating];});});}- (void)prepareForReuse{self.imageView.image = nil;}
  7. 7. 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 imageURLif ([self.currentImageURL isEqual:imageURL]) {UIImage *originalImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];UIImage *resizedImage = [originalImage resizedImageWithContentMode:UIViewContentModeScaleAspectFitbounds:self.imageView.bounds.sizeinterpolationQuality: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;}
  8. 8. José González Gómezjose.gonzalez@openinput.com@openinput

×