Skip to content

Commit

Permalink
Removed the decompression of images before display as this is no long…
Browse files Browse the repository at this point in the history
…er needed with faster devices about.

This significantly reduces memory and CPU requirements of the photo browser.

Closes mwaterfall#157 Closes mwaterfall#169
  • Loading branch information
mwaterfall committed Mar 13, 2014
1 parent b128d18 commit ce333b4
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions MWPhotoBrowser/Classes/MWPhoto.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ @interface MWPhoto () {

}

- (void)decompressImageAndFinishLoading;
- (void)imageLoadingComplete;

@end
Expand Down Expand Up @@ -94,16 +93,15 @@ - (void)loadUnderlyingImageAndNotify {
}
}

// Set the underlyingImage and call decompressImageAndFinishLoading on the main thread when complete.
// On error, set underlyingImage to nil and then call decompressImageAndFinishLoading on the main thread.
// Set the underlyingImage
- (void)performLoadUnderlyingImageAndNotify {

// Get underlying image
if (_image) {

// We have UIImage so decompress
// We have UIImage!
self.underlyingImage = _image;
[self decompressImageAndFinishLoading];
[self imageLoadingComplete];

} else if (_photoURL) {

Expand All @@ -122,16 +120,16 @@ - (void)performLoadUnderlyingImageAndNotify {
if (iref) {
self.underlyingImage = [UIImage imageWithCGImage:iref];
}
[self performSelectorOnMainThread:@selector(decompressImageAndFinishLoading) withObject:nil waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
}
failureBlock:^(NSError *error) {
self.underlyingImage = nil;
MWLog(@"Photo from asset library error: %@",error);
[self performSelectorOnMainThread:@selector(decompressImageAndFinishLoading) withObject:nil waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
}];
} @catch (NSException *e) {
MWLog(@"Photo from asset library error: %@", e);
[self performSelectorOnMainThread:@selector(decompressImageAndFinishLoading) withObject:nil waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
}
}
});
Expand All @@ -147,7 +145,7 @@ - (void)performLoadUnderlyingImageAndNotify {
MWLog(@"Error loading photo from path: %@", _photoURL.path);
}
} @finally {
[self performSelectorOnMainThread:@selector(decompressImageAndFinishLoading) withObject:nil waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
}
}
});
Expand All @@ -174,12 +172,12 @@ - (void)performLoadUnderlyingImageAndNotify {
}
_webImageOperation = nil;
self.underlyingImage = image;
[self decompressImageAndFinishLoading];
[self imageLoadingComplete];
}];
} @catch (NSException *e) {
MWLog(@"Photo from web: %@", e);
_webImageOperation = nil;
[self decompressImageAndFinishLoading];
[self imageLoadingComplete];
}

}
Expand All @@ -195,26 +193,7 @@ - (void)performLoadUnderlyingImageAndNotify {
// Release if we can get it again from path or url
- (void)unloadUnderlyingImage {
_loadingInProgress = NO;
if (self.underlyingImage) {
self.underlyingImage = nil;
}
}

- (void)decompressImageAndFinishLoading {
NSAssert([[NSThread currentThread] isMainThread], @"This method must be called on the main thread.");
if (self.underlyingImage) {
// Decode image async to avoid lagging when UIKit lazy loads
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.underlyingImage = [UIImage decodedImageWithImage:self.underlyingImage];
dispatch_async(dispatch_get_main_queue(), ^{
// Finish on main thread
[self imageLoadingComplete];
});
});
} else {
// Failed
[self imageLoadingComplete];
}
self.underlyingImage = nil;
}

- (void)imageLoadingComplete {
Expand Down

0 comments on commit ce333b4

Please sign in to comment.