Weitere ähnliche Inhalte Kürzlich hochgeladen (20) The Doors - Building a performant media viewer with GCD4. THE DOORS
Standing on the shoulders of giants
GMGridView
https://github.com/gmoledina/GMGridView
iCarousel
https://github.com/nicklockwood/iCarousel
UIImage categories
https://github.com/jchatard/UIImage-categories
5. THE DOORS
The newbie solution
- (void)setImageURL:(NSURL *)imageURL
{
self.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
}
- (void)prepareForReuse
{
self.imageView.image = nil;
}
6. THE DOORS
The 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;
}
7. THE DOORS
The 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;
}