Skip to content

Commit

Permalink
修改支持GIF
Browse files Browse the repository at this point in the history
  • Loading branch information
乔克 committed Sep 4, 2022
1 parent 319db51 commit 6cc928e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Pod/Classes/MWGridViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
cell.selectionMode = _selectionMode;
cell.isSelected = [_browser photoIsSelectedAtIndex:indexPath.row];
cell.index = indexPath.row;
UIImage *img = [_browser imageForPhoto:photo];
id img = [_browser imageForPhoto:photo];
if (img) {
[cell displayImage];
} else {
Expand Down
4 changes: 4 additions & 0 deletions Pod/Classes/MWPhoto.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
@property (nonatomic, strong) NSURL *videoURL;
@property (nonatomic) BOOL emptyImage;
@property (nonatomic) BOOL isVideo;
@property (nonatomic, readonly) UIImage *image;
@property (nonatomic, readonly) NSData *data;
@property (nonatomic, readonly) NSURL *photoURL;
@property (nonatomic, readonly) NSString *filePath __attribute__((deprecated("Use photoURL"))); // Depreciated

+ (MWPhoto *)photoWithImage:(UIImage *)image;
+ (MWPhoto *)photoWithURL:(NSURL *)url;
Expand Down
22 changes: 18 additions & 4 deletions Pod/Classes/MWPhoto.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import <AssetsLibrary/AssetsLibrary.h>
#import "MWPhoto.h"
#import "MWPhotoBrowser.h"
#import "UIImage+GIF.h"

@interface MWPhoto () {

Expand All @@ -34,6 +35,7 @@ - (void)imageLoadingComplete;
@implementation MWPhoto

@synthesize underlyingImage = _underlyingImage; // synth property from protocol
@synthesize underlyingImageData = _underlyingImageData; // synth property from protocol

#pragma mark - Class Methods

Expand Down Expand Up @@ -145,19 +147,24 @@ - (UIImage *)underlyingImage {
return _underlyingImage;
}

- (NSData *)underlyingImageData {
return _underlyingImageData;
}

- (void)loadUnderlyingImageAndNotify {
NSAssert([[NSThread currentThread] isMainThread], @"This method must be called on the main thread.");
if (_loadingInProgress) return;
_loadingInProgress = YES;
@try {
if (self.underlyingImage) {
if (self.underlyingImage || self.underlyingImageData) {
[self imageLoadingComplete];
} else {
[self performLoadUnderlyingImageAndNotify];
}
}
@catch (NSException *exception) {
self.underlyingImage = nil;
self.underlyingImageData = nil;
_loadingInProgress = NO;
[self imageLoadingComplete];
}
Expand All @@ -169,10 +176,11 @@ - (void)loadUnderlyingImageAndNotify {
- (void)performLoadUnderlyingImageAndNotify {

// Get underlying image
if (_image) {
if (_image || _data) {

// We have UIImage!
self.underlyingImage = _image;
self.underlyingImageData = _data;
[self imageLoadingComplete];

} else if (_photoURL) {
Expand Down Expand Up @@ -251,7 +259,12 @@ - (void)_performLoadUnderlyingImageAndNotifyWithWebURL:(NSURL *)url {
MWLog(@"SDWebImage failed to download image: %@", error);
}
_webImageOperation = nil;
self.underlyingImage = image;

if ([[url.absoluteString lowercaseString] containsString:@"gif"]) {
self.underlyingImageData = data;
} else {
self.underlyingImage = image;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self imageLoadingComplete];
});
Expand All @@ -268,6 +281,7 @@ - (void)_performLoadUnderlyingImageAndNotifyWithLocalFileURL:(NSURL *)url {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@autoreleasepool {
@try {
//TODO: FIXME need fix gif on local maybe
self.underlyingImage = [UIImage imageWithContentsOfFile:url.path];
if (!_underlyingImage) {
MWLog(@"Error loading photo from path: %@", url.path);
Expand Down Expand Up @@ -336,7 +350,7 @@ - (void)_performLoadUnderlyingImageAndNotifyWithAsset:(PHAsset *)asset targetSiz
// Release if we can get it again from path or url
- (void)unloadUnderlyingImage {
_loadingInProgress = NO;
self.underlyingImage = nil;
// self.underlyingImage = nil;
}

- (void)imageLoadingComplete {
Expand Down
8 changes: 5 additions & 3 deletions Pod/Classes/MWPhotoBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -708,12 +708,14 @@ - (void)setPhotoSelected:(BOOL)selected atIndex:(NSUInteger)index {
}
}

- (UIImage *)imageForPhoto:(id<MWPhoto>)photo {
- (id)imageForPhoto:(id<MWPhoto>)photo {
if (photo) {
// Get image or obtain in background
if ([photo underlyingImage]) {
return [photo underlyingImage];
} else {
} else if ([photo underlyingImageData]) {
return [photo underlyingImageData];
} else {
[photo loadUnderlyingImageAndNotify];
}
}
Expand Down Expand Up @@ -752,7 +754,7 @@ - (void)handleMWPhotoLoadingDidEndNotification:(NSNotification *)notification {
id <MWPhoto> photo = [notification object];
MWZoomingScrollView *page = [self pageDisplayingPhoto:photo];
if (page) {
if ([photo underlyingImage]) {
if ([photo underlyingImage] || [photo underlyingImageData]) {
// Successful load
[page displayImage];
[self loadAdjacentPhotosIfNecessary:photo];
Expand Down
3 changes: 2 additions & 1 deletion Pod/Classes/MWPhotoProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// in -loadUnderlyingImageAndNotify: which may be called by the photo browser if this
// methods returns nil.
@property (nonatomic, strong) UIImage *underlyingImage;
@property (nonatomic, strong) NSData *underlyingImageData;

// Called when the browser has determined the underlying images is not
// already loaded into memory but needs it.
Expand Down Expand Up @@ -68,4 +69,4 @@
// Cancel any background loading of image data
- (void)cancelAnyLoading;

@end
@end
6 changes: 4 additions & 2 deletions Pod/Classes/MWTapDetectingImageView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
//

#import <Foundation/Foundation.h>
#import "FLAnimatedImageView+WebCache.h"


@protocol MWTapDetectingImageViewDelegate;

@interface MWTapDetectingImageView : UIImageView {}
@interface MWTapDetectingImageView : FLAnimatedImageView {}

@property (nonatomic, weak) id <MWTapDetectingImageViewDelegate> tapDelegate;

Expand All @@ -24,4 +26,4 @@
- (void)imageView:(UIImageView *)imageView doubleTapDetected:(UITouch *)touch;
- (void)imageView:(UIImageView *)imageView tripleTapDetected:(UITouch *)touch;

@end
@end
32 changes: 23 additions & 9 deletions Pod/Classes/MWZoomingScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ - (void)prepareForReuse {
self.playButton = nil;
_photoImageView.hidden = NO;
_photoImageView.image = nil;
_photoImageView.animatedImage = nil;
_index = NSUIntegerMax;
}

Expand All @@ -113,7 +114,7 @@ - (void)setPhoto:(id<MWPhoto>)photo {
}
}
_photo = photo;
UIImage *img = [_photoBrowser imageForPhoto:_photo];
id img = [_photoBrowser imageForPhoto:_photo];
if (img) {
[self displayImage];
} else {
Expand All @@ -124,7 +125,7 @@ - (void)setPhoto:(id<MWPhoto>)photo {

// Get and display image
- (void)displayImage {
if (_photo && _photoImageView.image == nil) {
if (_photo && _photoImageView.animatedImage == nil) {

// Reset
self.maximumZoomScale = 1;
Expand All @@ -133,20 +134,27 @@ - (void)displayImage {
self.contentSize = CGSizeMake(0, 0);

// Get image from browser as it handles ordering of fetching
UIImage *img = [_photoBrowser imageForPhoto:_photo];
id img = [_photoBrowser imageForPhoto:_photo];
if (img) {

// Hide indicator
[self hideLoadingIndicator];

CGRect photoImageViewFrame;

// Set image
_photoImageView.image = img;
if ([img isKindOfClass:[NSData class]]) {
FLAnimatedImage *image = [[FLAnimatedImage alloc] initWithAnimatedGIFData:img];
_photoImageView.animatedImage = image;
photoImageViewFrame.size = image.posterImage.size;
} else {
_photoImageView.image = img;
photoImageViewFrame.size = ((UIImage *)img).size;
}
_photoImageView.hidden = NO;

// Setup photo frame
CGRect photoImageViewFrame;
photoImageViewFrame.origin = CGPointZero;
photoImageViewFrame.size = img.size;
_photoImageView.frame = photoImageViewFrame;
self.contentSize = photoImageViewFrame.size;

Expand All @@ -167,6 +175,7 @@ - (void)displayImage {
- (void)displayImageFailure {
[self hideLoadingIndicator];
_photoImageView.image = nil;
_photoImageView.animatedImage = nil;

// Show if image is not empty
if (![_photo respondsToSelector:@selector(emptyImage)] || !_photo.emptyImage) {
Expand Down Expand Up @@ -226,7 +235,7 @@ - (CGFloat)initialZoomScaleWithMinScale {
if (_photoImageView && _photoBrowser.zoomPhotosToFill) {
// Zoom image to fill if the aspect ratios are fairly similar
CGSize boundsSize = self.bounds.size;
CGSize imageSize = _photoImageView.image.size;
CGSize imageSize = _photoImageView.animatedImage.size;
CGFloat boundsAR = boundsSize.width / boundsSize.height;
CGFloat imageAR = imageSize.width / imageSize.height;
CGFloat xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise
Expand All @@ -249,14 +258,19 @@ - (void)setMaxMinZoomScalesForCurrentBounds {
self.zoomScale = 1;

// Bail if no image
if (_photoImageView.image == nil) return;
if (_photoImageView.animatedImage == nil && _photoImageView.image == nil) return;

// Reset position
_photoImageView.frame = CGRectMake(0, 0, _photoImageView.frame.size.width, _photoImageView.frame.size.height);

// Sizes
CGSize boundsSize = self.bounds.size;
CGSize imageSize = _photoImageView.image.size;
CGSize imageSize;
if (_photoImageView.animatedImage) {
imageSize = _photoImageView.animatedImage.size;
} else {
imageSize = _photoImageView.image.size;
}

// Calculate Min
CGFloat xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise
Expand Down

0 comments on commit 6cc928e

Please sign in to comment.