diff --git a/Classes/IDMPhoto.h b/Classes/IDMPhoto.h index f87bd178..44b4a5f0 100644 --- a/Classes/IDMPhoto.h +++ b/Classes/IDMPhoto.h @@ -8,7 +8,6 @@ #import #import "IDMPhotoProtocol.h" -#import // This class models a photo/image and it's caption // If you want to handle photos, caching, decompression diff --git a/Classes/IDMPhoto.m b/Classes/IDMPhoto.m index a2959d5c..a1df6c20 100644 --- a/Classes/IDMPhoto.m +++ b/Classes/IDMPhoto.m @@ -8,6 +8,7 @@ #import "IDMPhoto.h" #import "IDMPhotoBrowser.h" +#import // Private @interface IDMPhoto () { @@ -34,7 +35,7 @@ - (void)imageLoadingComplete; @implementation IDMPhoto // Properties -@synthesize underlyingImage = _underlyingImage, +@synthesize underlyingImage = _underlyingImage, photoURL = _photoURL, caption = _caption; @@ -54,33 +55,33 @@ + (IDMPhoto *)photoWithURL:(NSURL *)url { + (NSArray *)photosWithImages:(NSArray *)imagesArray { NSMutableArray *photos = [NSMutableArray arrayWithCapacity:imagesArray.count]; - + for (UIImage *image in imagesArray) { if ([image isKindOfClass:[UIImage class]]) { IDMPhoto *photo = [IDMPhoto photoWithImage:image]; [photos addObject:photo]; } } - + return photos; } + (NSArray *)photosWithFilePaths:(NSArray *)pathsArray { NSMutableArray *photos = [NSMutableArray arrayWithCapacity:pathsArray.count]; - + for (NSString *path in pathsArray) { if ([path isKindOfClass:[NSString class]]) { IDMPhoto *photo = [IDMPhoto photoWithFilePath:path]; [photos addObject:photo]; } } - + return photos; } + (NSArray *)photosWithURLs:(NSArray *)urlsArray { NSMutableArray *photos = [NSMutableArray arrayWithCapacity:urlsArray.count]; - + for (id url in urlsArray) { if ([url isKindOfClass:[NSURL class]]) { IDMPhoto *photo = [IDMPhoto photoWithURL:url]; @@ -91,7 +92,7 @@ + (NSArray *)photosWithURLs:(NSArray *)urlsArray { [photos addObject:photo]; } } - + return photos; } @@ -136,20 +137,32 @@ - (void)loadUnderlyingImageAndNotify { [self performSelectorInBackground:@selector(loadImageFromFileAsync) withObject:nil]; } else if (_photoURL) { // Load async from web (using SDWebImageManager) - - [[SDWebImageManager sharedManager] loadImageWithURL:_photoURL options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) { - CGFloat progress = ((CGFloat)receivedSize)/((CGFloat)expectedSize); - - if (self.progressUpdateBlock) { - self.progressUpdateBlock(progress); - } - } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) { - if (image) { - self.underlyingImage = image; - } - - [self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO]; - }]; + + [[YYWebImageManager sharedManager] requestImageWithURL:_photoURL options:YYWebImageOptionShowNetworkActivity progress:^(NSInteger receivedSize, NSInteger expectedSize) { + if ([NSThread isMainThread]) { + CGFloat progress = ((CGFloat)receivedSize)/((CGFloat)expectedSize); + if (self.progressUpdateBlock) { + self.progressUpdateBlock(progress); + } + } else { + __weak typeof(self) weakSelf = self; + dispatch_async(dispatch_get_main_queue(), ^{ + CGFloat progress = ((CGFloat)receivedSize)/((CGFloat)expectedSize); + if (weakSelf.progressUpdateBlock) { + weakSelf.progressUpdateBlock(progress); + } + }); + } + } transform:^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) { + return image; + } completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) { + if (image) { + self.underlyingImage = image; + } + + [self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO]; + }]; + } else { // Failed - no source self.underlyingImage = nil; @@ -174,7 +187,7 @@ - (void)unloadUnderlyingImage { // System only supports RGB, set explicitly and prevent context error // if the downloaded image is not the supported format CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); - + CGContextRef context = CGBitmapContextCreate(NULL, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef), @@ -186,16 +199,16 @@ - (void)unloadUnderlyingImage { // makes system don't need to do extra conversion when displayed. kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little); CGColorSpaceRelease(colorSpace); - + if ( ! context) { return nil; } - + CGRect rect = (CGRect){CGPointZero, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)}; CGContextDrawImage(context, rect, imageRef); CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context); CGContextRelease(context); - + UIImage *decompressedImage = [[UIImage alloc] initWithCGImage:decompressedImageRef]; CGImageRelease(decompressedImageRef); return decompressedImage; @@ -206,26 +219,26 @@ - (UIImage *)decodedImageWithImage:(UIImage *)image { // Do not decode animated images return image; } - + CGImageRef imageRef = image.CGImage; CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize}; - + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); - + int infoMask = (bitmapInfo & kCGBitmapAlphaInfoMask); BOOL anyNonAlpha = (infoMask == kCGImageAlphaNone || infoMask == kCGImageAlphaNoneSkipFirst || infoMask == kCGImageAlphaNoneSkipLast); - + // CGBitmapContextCreate doesn't support kCGImageAlphaNone with RGB. // https://developer.apple.com/library/mac/#qa/qa1037/_index.html if (infoMask == kCGImageAlphaNone && CGColorSpaceGetNumberOfComponents(colorSpace) > 1) { // Unset the old alpha info. bitmapInfo &= ~kCGBitmapAlphaInfoMask; - + // Set noneSkipFirst. bitmapInfo |= kCGImageAlphaNoneSkipFirst; } @@ -236,7 +249,7 @@ - (UIImage *)decodedImageWithImage:(UIImage *)image { bitmapInfo &= ~kCGBitmapAlphaInfoMask; bitmapInfo |= kCGImageAlphaPremultipliedFirst; } - + // It calculates the bytes-per-row based on the bitsPerComponent and width arguments. CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width, @@ -246,15 +259,15 @@ - (UIImage *)decodedImageWithImage:(UIImage *)image { colorSpace, bitmapInfo); CGColorSpaceRelease(colorSpace); - + // If failed, return undecompressed image if (!context) return image; - + CGContextDrawImage(context, imageRect, imageRef); CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context); - + CGContextRelease(context); - + UIImage *decompressedImage = [UIImage imageWithCGImage:decompressedImageRef scale:image.scale orientation:image.imageOrientation]; CGImageRelease(decompressedImageRef); return decompressedImage; diff --git a/Classes/IDMTapDetectingImageView.h b/Classes/IDMTapDetectingImageView.h index 96a3865e..dfe11e42 100644 --- a/Classes/IDMTapDetectingImageView.h +++ b/Classes/IDMTapDetectingImageView.h @@ -7,10 +7,11 @@ // #import +#import @protocol IDMTapDetectingImageViewDelegate; -@interface IDMTapDetectingImageView : UIImageView { +@interface IDMTapDetectingImageView : YYAnimatedImageView { id __weak tapDelegate; } @property (nonatomic, weak) id tapDelegate; @@ -24,4 +25,4 @@ - (void)imageView:(UIImageView *)imageView singleTapDetected:(UITouch *)touch; - (void)imageView:(UIImageView *)imageView doubleTapDetected:(UITouch *)touch; - (void)imageView:(UIImageView *)imageView tripleTapDetected:(UITouch *)touch; -@end \ No newline at end of file +@end diff --git a/Demo/Podfile b/Demo/Podfile index cb335507..1d4a0f5b 100644 --- a/Demo/Podfile +++ b/Demo/Podfile @@ -5,7 +5,7 @@ inhibit_all_warnings! target "PhotoBrowserDemo" do -pod 'SDWebImage' +pod 'YYWebImage' pod 'DACircularProgress' pod 'pop' diff --git a/IDMPhotoBrowser.podspec b/IDMPhotoBrowser.podspec index b2e724f4..c3042438 100644 --- a/IDMPhotoBrowser.podspec +++ b/IDMPhotoBrowser.podspec @@ -11,7 +11,7 @@ Pod::Spec.new do |s| s.resources = 'Classes/IDMPhotoBrowser.bundle', 'Classes/IDMPBLocalizations.bundle' s.framework = 'MessageUI', 'QuartzCore', 'SystemConfiguration', 'MobileCoreServices', 'Security' s.requires_arc = true - s.dependency 'SDWebImage' + s.dependency 'YYKit' s.dependency 'DACircularProgress' s.dependency 'pop' end diff --git a/README.markdown b/README.markdown index 5549275a..c53b87b6 100644 --- a/README.markdown +++ b/README.markdown @@ -6,7 +6,7 @@ We've added both user experience and technical features inspired by Facebook's a ## New features: - Uses ARC -- Uses SDWebImage for image loading +- Uses YYWebImage for image loading - Image progress shown - Minimalistic Facebook-like interface, swipe up/down to dismiss - Ability to add custom actions on the action sheet @@ -181,7 +181,7 @@ Just add `pod 'IDMPhotoBrowser'` to your Podfile. #### Opensource libraries used -- [SDWebImage](https://github.com/rs/SDWebImage) +- [YYKit](https://github.com/ibireme/YYKit) - [DACircularProgress](https://github.com/danielamitay/DACircularProgress) - [pop](https://github.com/facebook/pop)