MJPhotoView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // MJZoomingScrollView.m
  3. //
  4. // Created by mj on 13-3-4.
  5. // Copyright (c) 2013年 itcast. All rights reserved.
  6. //
  7. #import "MJPhotoView.h"
  8. #import "MJPhoto.h"
  9. #import "MJPhotoLoadingView.h"
  10. #import "UIImageView+WebCache.h"
  11. #import <QuartzCore/QuartzCore.h>
  12. #import "JCHATChatModel.h"
  13. #import "JChatConstants.h"
  14. @interface MJPhotoView ()
  15. {
  16. BOOL _doubleTap;
  17. UIImageView *_imageView;
  18. MJPhotoLoadingView *_photoLoadingView;
  19. }
  20. @end
  21. @implementation MJPhotoView
  22. - (id)initWithFrame:(CGRect)frame
  23. {
  24. if ((self = [super initWithFrame:frame])) {
  25. self.clipsToBounds = YES;
  26. // 图片
  27. _imageView = [[UIImageView alloc] init];
  28. [self addSubview:_imageView];
  29. // 进度条
  30. _photoLoadingView = [[MJPhotoLoadingView alloc] init];
  31. // 属性
  32. self.backgroundColor = [UIColor clearColor];
  33. self.delegate = self;
  34. self.showsHorizontalScrollIndicator = NO;
  35. self.showsVerticalScrollIndicator = NO;
  36. self.decelerationRate = UIScrollViewDecelerationRateFast;
  37. self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  38. // 监听点击
  39. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
  40. singleTap.delaysTouchesBegan = YES;
  41. singleTap.numberOfTapsRequired = 1;
  42. [self addGestureRecognizer:singleTap];
  43. UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
  44. doubleTap.numberOfTapsRequired = 2;
  45. [self addGestureRecognizer:doubleTap];
  46. }
  47. return self;
  48. }
  49. #pragma mark - photoSetter
  50. - (void)setPhoto:(MJPhoto *)photo {
  51. _photo = photo;
  52. [self showImage];
  53. }
  54. #pragma mark 显示图片
  55. - (void)showImage
  56. {
  57. if (_photo.firstShow) { // 首次显示
  58. _imageView.image = _photo.placeholder; // 占位图片
  59. _photo.srcImageView.image = nil;
  60. // 不是gif,就马上开始下载
  61. if (![_photo.url.absoluteString hasSuffix:@"gif"]) {
  62. __weak MJPhoto *photo = _photo;
  63. __weak MJPhotoView *photoView = self;
  64. if (_photo.message.message.msgId) {
  65. JMSGMessage *message = [_conversation messageWithMessageId:_photo.message.message.msgId];
  66. [((JMSGImageContent *)message.content) largeImageDataWithProgress:^(float percent,NSString *msgId){
  67. _photoLoadingView.progress = percent;
  68. } completionHandler:^(NSData *data,NSString *objectId, NSError *error) {
  69. if (error == nil) {
  70. NSLog(@"下载大图 success the image message id is %@",objectId);
  71. photo.image = [UIImage imageWithData:data];
  72. _imageView.image = [UIImage imageWithData:data];
  73. [photoView adjustFrame];
  74. } else {
  75. JPIMLog(@"下载大图 error");
  76. //[MBProgressHUD showMessage:@"下载大图失败!" view:self];
  77. [MBProgressHUD showMessage:YZMsg(@"下载大图失败!") toView:self];
  78. [photoView adjustFrame];
  79. }
  80. }];
  81. } else {
  82. JPIMLog(@"messageid is nil");
  83. }
  84. }
  85. } else {
  86. [self photoStartLoad];
  87. }
  88. // 调整frame参数
  89. [self adjustFrame];
  90. }
  91. #pragma mark 开始加载图片
  92. - (void)photoStartLoad
  93. {
  94. self.scrollEnabled = NO;
  95. // 直接显示进度条
  96. _imageView.image = _photo.placeholder; // 占位图片
  97. [_photoLoadingView showLoading];
  98. [self addSubview:_photoLoadingView];
  99. __weak MJPhotoView *photoView = self;
  100. __weak MJPhoto *photo = _photo;
  101. if (_photo.message.message) {
  102. JMSGMessage *message = _photo.message.message;
  103. [((JMSGImageContent *)message.content) thumbImageData:^(NSData *data, NSString *objectId, NSError *error) {
  104. if (error == nil) {
  105. _imageView.image = [UIImage imageWithData:data];
  106. }
  107. }];
  108. [((JMSGImageContent *)message.content) largeImageDataWithProgress:^(float percent,NSString *msgId){
  109. _photoLoadingView.progress = percent;
  110. } completionHandler:^(NSData *data,NSString *objectId, NSError *error) {
  111. __strong __typeof(photo)strongPhoto = photo;
  112. if (error == nil) {
  113. NSLog(@"下载大图 success the image message id is %@",objectId);
  114. strongPhoto.image = [UIImage imageWithData:data];
  115. [photoView photoDidFinishLoadWithImage:strongPhoto.image];
  116. _imageView.image = strongPhoto.image;
  117. [photoView adjustFrame];
  118. } else {
  119. //[MBProgressHUD showMessage:@"下载大图失败!!" view:self];
  120. [MBProgressHUD showMessage:YZMsg(@"下载大图失败!") toView:self];
  121. }
  122. }];
  123. } else {
  124. JPIMLog(@"messageid is nil");
  125. }
  126. }
  127. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  128. {
  129. if ([keyPath isEqualToString:@"fractionCompleted"] && [object isKindOfClass:[NSProgress class]]) {
  130. NSProgress *progress = (NSProgress *)object;
  131. NSLog(@"Progress is %f", progress.fractionCompleted);
  132. _photoLoadingView.progress = progress.fractionCompleted;
  133. }
  134. }
  135. #pragma mark 加载完毕
  136. - (void)photoDidFinishLoadWithImage:(UIImage *)image
  137. {
  138. if (image) {
  139. self.scrollEnabled = YES;
  140. _photo.image = image;
  141. [_photoLoadingView removeFromSuperview];
  142. if ([self.photoViewDelegate respondsToSelector:@selector(photoViewImageFinishLoad:)]) {
  143. [self.photoViewDelegate photoViewImageFinishLoad:self];
  144. }
  145. } else {
  146. [self addSubview:_photoLoadingView];
  147. [_photoLoadingView showFailure];
  148. }
  149. // 设置缩放比例
  150. [self adjustFrame];
  151. }
  152. #pragma mark 调整frame
  153. - (void)adjustFrame
  154. {
  155. if (_imageView.image == nil) return;
  156. // 基本尺寸参数
  157. CGSize boundsSize = self.bounds.size;
  158. CGFloat boundsWidth = boundsSize.width;
  159. CGFloat boundsHeight = boundsSize.height;
  160. CGSize imageSize = _imageView.image.size;
  161. CGFloat imageWidth = imageSize.width;
  162. CGFloat imageHeight = imageSize.height;
  163. // 设置伸缩比例
  164. CGFloat widthRatio = boundsWidth/imageWidth;
  165. CGFloat heightRatio = boundsHeight/imageHeight;
  166. CGFloat minScale = (widthRatio > heightRatio) ? heightRatio : widthRatio;
  167. if (minScale >= 1) {
  168. minScale = 0.8;
  169. }
  170. CGFloat maxScale = 4.0;
  171. self.maximumZoomScale = maxScale;
  172. self.minimumZoomScale = minScale;
  173. self.zoomScale = minScale;
  174. CGRect imageFrame = CGRectMake(0, 0, boundsWidth, imageHeight * boundsWidth / imageWidth);
  175. // 内容尺寸
  176. self.contentSize = CGSizeMake(0, imageFrame.size.height);
  177. // 宽大
  178. if ( imageWidth <= imageHeight && imageHeight < boundsHeight ) {
  179. imageFrame.origin.x = floorf( (boundsWidth - imageFrame.size.width ) / 2.0) * minScale;
  180. imageFrame.origin.y = floorf( (boundsHeight - imageFrame.size.height ) / 2.0) * minScale;
  181. } else{
  182. imageFrame.origin.x = floorf( (boundsWidth - imageFrame.size.width ) / 2.0);
  183. imageFrame.origin.y = floorf( (boundsHeight - imageFrame.size.height ) / 2.0);
  184. }
  185. // // y值
  186. // if (imageFrame.size.height < boundsHeight) {
  187. //
  188. // imageFrame.origin.y = floorf( (boundsHeight - imageFrame.size.height ) / 2.0) * minScale;
  189. //
  190. //// imageFrame.origin.y = floorf( (boundsHeight - imageFrame.size.height ) / 2.0) * minScale;
  191. //
  192. // } else {
  193. // imageFrame.origin.y = 0;
  194. // }
  195. if (_photo.firstShow) { // 第一次显示的图片
  196. _photo.firstShow = NO; // 已经显示过了
  197. _imageView.frame = [_photo.srcImageView convertRect:_photo.srcImageView.bounds toView:nil];
  198. [UIView animateWithDuration: 0.3 animations:^{
  199. _imageView.frame = imageFrame;
  200. } completion:^(BOOL finished) {
  201. // 设置底部的小图片
  202. _photo.srcImageView.image = _photo.placeholder;
  203. [self photoStartLoad];
  204. }];
  205. } else {
  206. _imageView.frame = imageFrame;
  207. }
  208. }
  209. #pragma mark - UIScrollViewDelegate
  210. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  211. return _imageView;
  212. }
  213. // 让UIImageView在UIScrollView缩放后居中显示
  214. - (void)scrollViewDidZoom:(UIScrollView *)scrollView
  215. {
  216. CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
  217. (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
  218. CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
  219. (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
  220. _imageView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
  221. scrollView.contentSize.height * 0.5 + offsetY);
  222. }
  223. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  224. {
  225. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  226. }
  227. #pragma mark - 手势处理
  228. - (void)handleSingleTap:(UITapGestureRecognizer *)tap {
  229. _doubleTap = NO;
  230. [self performSelector:@selector(hide) withObject:nil afterDelay:0.2];
  231. }
  232. - (void)hide
  233. {
  234. if (_doubleTap) return;
  235. // 移除进度条
  236. [_photoLoadingView removeFromSuperview];
  237. self.contentOffset = CGPointZero;
  238. // 清空底部的小图
  239. _photo.srcImageView.image = nil;
  240. CGFloat duration = 0.15;
  241. if (_photo.srcImageView.clipsToBounds) {
  242. [self performSelector:@selector(reset) withObject:nil afterDelay:duration];
  243. }
  244. [UIView animateWithDuration:duration + 0.1 animations:^{
  245. _imageView.frame = [_photo.srcImageView convertRect:_photo.srcImageView.bounds toView:nil];
  246. // gif图片仅显示第0张
  247. if (_imageView.image.images) {
  248. _imageView.image = _imageView.image.images[0];
  249. }
  250. // 通知代理
  251. if ([self.photoViewDelegate respondsToSelector:@selector(photoViewSingleTap:)]) {
  252. [self.photoViewDelegate photoViewSingleTap:self];
  253. }
  254. } completion:^(BOOL finished) {
  255. // 设置底部的小图片
  256. _photo.srcImageView.image = _photo.placeholder;
  257. // 通知代理
  258. if ([self.photoViewDelegate respondsToSelector:@selector(photoViewDidEndZoom:)]) {
  259. [self.photoViewDelegate photoViewDidEndZoom:self];
  260. }
  261. }];
  262. }
  263. - (void)reset
  264. {
  265. _imageView.image = _photo.capture;
  266. _imageView.contentMode = UIViewContentModeScaleToFill;
  267. }
  268. - (void)handleDoubleTap:(UITapGestureRecognizer *)tap {
  269. _doubleTap = YES;
  270. CGPoint touchPoint = [tap locationInView:self];
  271. if (self.zoomScale == self.maximumZoomScale) {
  272. [self setZoomScale:self.minimumZoomScale animated:YES];
  273. } else {
  274. [self zoomToRect:CGRectMake(touchPoint.x, touchPoint.y, 1, 1) animated:YES];
  275. }
  276. }
  277. - (void)dealloc
  278. {
  279. // 取消请求
  280. // [_imageView setImageWithURL:[NSURL URLWithString:@"file:///abc"]];
  281. [_imageView sd_setImageWithURL:[NSURL URLWithString:@"file:///abc"]];
  282. }
  283. @end