88#import " MJPhotoView.h"
99#import " MJPhoto.h"
1010#import " MJPhotoLoadingView.h"
11- #import < SDWebImage/UIImageView+WebCache.h>
1211#import < QuartzCore/QuartzCore.h>
12+ #import < YLGIFImage/YLGIFImage.h>
13+ #import < YLGIFImage/YLImageView.h>
1314
1415@interface MJPhotoView ()
1516{
1617 BOOL _doubleTap;
17- UIImageView *_imageView;
18+ YLImageView *_imageView;
1819 MJPhotoLoadingView *_photoLoadingView;
1920}
2021@end
@@ -26,7 +27,7 @@ - (id)initWithFrame:(CGRect)frame
2627 if ((self = [super initWithFrame: frame])) {
2728 self.clipsToBounds = YES ;
2829 // 图片
29- _imageView = [[UIImageView alloc ] init ];
30+ _imageView = [[YLImageView alloc ] init ];
3031 _imageView.contentMode = UIViewContentModeScaleAspectFit;
3132 [self addSubview: _imageView];
3233
@@ -54,6 +55,12 @@ - (id)initWithFrame:(CGRect)frame
5455 return self;
5556}
5657
58+ // 设置imageView的图片
59+ - (void )configImageViewWithImage : (UIImage *)image {
60+ _imageView.image = image;
61+ }
62+
63+
5764#pragma mark - photoSetter
5865- (void )setPhoto : (MJPhoto *)photo {
5966 _photo = photo;
@@ -65,29 +72,33 @@ - (void)setPhoto:(MJPhoto *)photo {
6572- (void )showImage
6673{
6774 if (_photo.firstShow ) { // 首次显示
68- _imageView.image = _photo.placeholder ; // 占位图片
69-
75+ _imageView.image = _photo.placeholder ;
7076 if (_photo.image ) {
77+ _imageView.image = _photo.image ;
7178 if ([self .photoViewDelegate respondsToSelector: @selector (photoViewImageFinishLoad: )]) {
7279 [self .photoViewDelegate photoViewImageFinishLoad: self ];
7380 }
74- // 调整frame参数
75- [self adjustFrame ];
76- }else if (![_photo.url.absoluteString hasSuffix: @" gif" ]) {
77- // 不是gif,就马上开始下载
81+ }else {
82+ // 显示进度条
83+ [_photoLoadingView showLoading ];
84+ [self addSubview: _photoLoadingView];
85+
7886 ESWeakSelf;
79- ESWeak_ (_photo);
80- [_imageView sd_setImageWithURL: _photo.url placeholderImage: _photo.placeholder options: SDWebImageRetryFailed|SDWebImageLowPriority completed: ^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
87+ ESWeak_ (_photoLoadingView);
88+ ESWeak_ (_imageView);
89+
90+ [SDWebImageManager.sharedManager downloadImageWithURL: _photo.url options: SDWebImageRetryFailed|SDWebImageLowPriority progress: ^(NSInteger receivedSize, NSInteger expectedSize) {
91+ ESStrong_ (_photoLoadingView);
92+ if (receivedSize > kMinProgress ) {
93+ __photoLoadingView.progress = (float )receivedSize/expectedSize;
94+ }
95+ } completed: ^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
8196 ESStrongSelf;
82- ESStrong_ (_photo);
97+ ESStrong_ (_imageView)
8398 if (image) {
84- __photo .image = image;
99+ __imageView .image = image;
85100 }
86- if ([_self.photoViewDelegate respondsToSelector: @selector (photoViewImageFinishLoad: )]) {
87- [_self.photoViewDelegate photoViewImageFinishLoad: _self];
88- }
89- // 调整frame参数
90- [_self adjustFrame ];
101+ [_self photoDidFinishLoadWithImage: image];
91102 }];
92103 }
93104 } else {
@@ -102,23 +113,28 @@ - (void)showImage
102113- (void )photoStartLoad
103114{
104115 if (_photo.image ) {
105- self.scrollEnabled = YES ;
106116 _imageView.image = _photo.image ;
117+ self.scrollEnabled = YES ;
107118 } else {
119+ _imageView.image = _photo.placeholder ;
108120 self.scrollEnabled = NO ;
109121 // 直接显示进度条
110122 [_photoLoadingView showLoading ];
111123 [self addSubview: _photoLoadingView];
112124
113125 ESWeakSelf;
114126 ESWeak_ (_photoLoadingView);
115- [_imageView sd_setImageWithURL: _photo.url placeholderImage: _photo.srcImageView.image options: SDWebImageRetryFailed|SDWebImageLowPriority progress: ^(NSInteger receivedSize, NSInteger expectedSize) {
127+ ESWeak_ (_imageView);
128+
129+ [SDWebImageManager.sharedManager downloadImageWithURL: _photo.url options: SDWebImageRetryFailed|SDWebImageLowPriority progress: ^(NSInteger receivedSize, NSInteger expectedSize) {
116130 ESStrong_ (_photoLoadingView);
117131 if (receivedSize > kMinProgress ) {
118132 __photoLoadingView.progress = (float )receivedSize/expectedSize;
119133 }
120- } completed: ^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
134+ } completed: ^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
121135 ESStrongSelf;
136+ ESStrong_ (_imageView);
137+ __imageView.image = image;
122138 [_self photoDidFinishLoadWithImage: image];
123139 }];
124140 }
@@ -201,44 +217,22 @@ - (void)hide
201217 [_photoLoadingView removeFromSuperview ];
202218 self.contentOffset = CGPointZero;
203219
204- // 清空底部的小图
205- // _photo.srcImageView.image = nil;
206-
207220 CGFloat duration = 0.3 ;
208- if (_photo.srcImageView .clipsToBounds ) {
209- [self performSelector: @selector (reset ) withObject: nil afterDelay: duration];
210- }
211221 self.superview .backgroundColor = [UIColor clearColor ];
212222 [UIView animateWithDuration: duration + 0.1 animations: ^{
213223 self.alpha = 0.0 ;
214- // _imageView.frame = [_photo.srcImageView convertRect:_photo.srcImageView.bounds toView:nil];
215- //
216- // // gif图片仅显示第0张
217- // if (_imageView.image.images) {
218- // _imageView.image = _imageView.image.images[0];
219- // }
220-
221224 // 通知代理
222225 if ([self .photoViewDelegate respondsToSelector: @selector (photoViewSingleTap: )]) {
223226 [self .photoViewDelegate photoViewSingleTap: self ];
224227 }
225228 } completion: ^(BOOL finished) {
226- // 设置底部的小图片
227- // _photo.srcImageView.image = _photo.placeholder;
228-
229229 // 通知代理
230230 if ([self .photoViewDelegate respondsToSelector: @selector (photoViewDidEndZoom: )]) {
231231 [self .photoViewDelegate photoViewDidEndZoom: self ];
232232 }
233233 }];
234234}
235235
236- - (void )reset
237- {
238- // _imageView.image = _photo.capture;
239- // _imageView.contentMode = UIViewContentModeScaleToFill;
240- }
241-
242236- (void )handleDoubleTap : (UITapGestureRecognizer *)tap {
243237 _doubleTap = YES ;
244238
0 commit comments