SDPhotoBrowser.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // SDPhotoBrowser.m
  3. // photobrowser
  4. //
  5. // Created by aier on 15-2-3.
  6. // Copyright (c) 2015年 aier. All rights reserved.
  7. //
  8. #import "SDPhotoBrowser.h"
  9. #import "UIImageView+WebCache.h"
  10. #import "SDBrowserImageView.h"
  11. // ============在这里方便配置样式相关设置===========
  12. // ||
  13. // ||
  14. // ||
  15. // \\//
  16. // \/
  17. #import "SDPhotoBrowserConfig.h"
  18. // =============================================
  19. @implementation SDPhotoBrowser
  20. {
  21. UIScrollView *_scrollView;
  22. BOOL _hasShowedFistView;
  23. UILabel *_indexLabel;
  24. UIButton *_saveButton;
  25. UIActivityIndicatorView *_indicatorView;
  26. }
  27. - (id)initWithFrame:(CGRect)frame
  28. {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. self.backgroundColor = SDPhotoBrowserBackgrounColor;
  32. }
  33. return self;
  34. }
  35. - (void)didMoveToSuperview
  36. {
  37. [self setupScrollView];
  38. [self setupToolbars];
  39. }
  40. - (void)setupToolbars
  41. {
  42. float s = [[[UIApplication sharedApplication] delegate] window].frame.size.width / [[[UIApplication sharedApplication] delegate] window].frame.size.height;
  43. // 1. 序标
  44. UILabel *indexLabel = [[UILabel alloc] init];
  45. if(s < 0.5)
  46. {
  47. indexLabel.bounds = CGRectMake(0, 64, 80, 30);
  48. indexLabel.center = CGPointMake(self.bounds.size.width * 0.5, 44);
  49. }
  50. else
  51. {
  52. indexLabel.bounds = CGRectMake(0, 0, 80, 30);
  53. indexLabel.center = CGPointMake(self.bounds.size.width * 0.5, 30);
  54. }
  55. indexLabel.textAlignment = NSTextAlignmentCenter;
  56. indexLabel.textColor = [UIColor whiteColor];
  57. indexLabel.font = [UIFont boldSystemFontOfSize:20];
  58. indexLabel.backgroundColor = [UIColor clearColor];
  59. if (self.imageCount > 1) {
  60. indexLabel.text = [NSString stringWithFormat:@"1/%ld", (long)self.imageCount];
  61. }
  62. _indexLabel = indexLabel;
  63. [self addSubview:indexLabel];
  64. // 2.保存按钮
  65. UIButton *saveButton = [[UIButton alloc] init];
  66. [saveButton setTitle:@"保存" forState:UIControlStateNormal];
  67. [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  68. saveButton.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.90f];
  69. saveButton.frame = CGRectMake(30, self.bounds.size.height - 70, 50, 25);
  70. saveButton.layer.cornerRadius = 5;
  71. saveButton.clipsToBounds = YES;
  72. [saveButton addTarget:self action:@selector(saveImage) forControlEvents:UIControlEventTouchUpInside];
  73. _saveButton = saveButton;
  74. // [self addSubview:saveButton];
  75. }
  76. - (void)saveImage
  77. {
  78. int index = _scrollView.contentOffset.x / _scrollView.bounds.size.width;
  79. UIImageView *currentImageView = _scrollView.subviews[index];
  80. UIImageWriteToSavedPhotosAlbum(currentImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
  81. UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] init];
  82. indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
  83. indicator.center = self.center;
  84. _indicatorView = indicator;
  85. [[UIApplication sharedApplication].keyWindow addSubview:indicator];
  86. [indicator startAnimating];
  87. }
  88. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
  89. {
  90. [_indicatorView removeFromSuperview];
  91. UILabel *label = [[UILabel alloc] init];
  92. label.textColor = [UIColor whiteColor];
  93. label.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.90f];
  94. label.layer.cornerRadius = 5;
  95. label.clipsToBounds = YES;
  96. label.bounds = CGRectMake(0, 0, 150, 30);
  97. label.center = self.center;
  98. label.textAlignment = NSTextAlignmentCenter;
  99. label.font = [UIFont boldSystemFontOfSize:17];
  100. [[UIApplication sharedApplication].keyWindow addSubview:label];
  101. [[UIApplication sharedApplication].keyWindow bringSubviewToFront:label];
  102. if (error) {
  103. label.text = SDPhotoBrowserSaveImageFailText;
  104. } else {
  105. label.text = SDPhotoBrowserSaveImageSuccessText;
  106. }
  107. [label performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0];
  108. }
  109. - (void)setupScrollView
  110. {
  111. _scrollView = [[UIScrollView alloc] init];
  112. _scrollView.delegate = self;
  113. _scrollView.showsHorizontalScrollIndicator = NO;
  114. _scrollView.showsVerticalScrollIndicator = NO;
  115. [self addSubview:_scrollView];
  116. for (int i = 0; i < self.imageCount; i++) {
  117. SDBrowserImageView *imageView = [[SDBrowserImageView alloc] init];
  118. imageView.tag = i;
  119. [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoClick:)]];
  120. [_scrollView addSubview:imageView];
  121. }
  122. [self setupImageOfImageViewForIndex:self.currentImageIndex];
  123. }
  124. // 加载图片
  125. - (void)setupImageOfImageViewForIndex:(NSInteger)index
  126. {
  127. SDBrowserImageView *imageView = _scrollView.subviews[index];
  128. if (imageView.hasLoadedImage) return;
  129. if ([self highQualityImageURLForIndex:index]) {
  130. [imageView setImageWithURL:[self highQualityImageURLForIndex:index] placeholderImage:[self placeholderImageForIndex:index]];
  131. } else {
  132. imageView.image = [self placeholderImageForIndex:index];
  133. }
  134. imageView.hasLoadedImage = YES;
  135. }
  136. - (void)photoClick:(UITapGestureRecognizer *)recognizer
  137. {
  138. int index = (_scrollView.contentOffset.x + _scrollView.bounds.size.width * 0.5) / _scrollView.bounds.size.width;
  139. [self.delegate photoBrowser:self selectIndex:index];
  140. _scrollView.hidden = YES;
  141. SDBrowserImageView *currentImageView = (SDBrowserImageView *)recognizer.view;
  142. NSInteger currentIndex = currentImageView.tag;
  143. UIView *sourceView = self.sourceImagesContainerView.subviews[currentIndex];
  144. CGRect targetTemp = [self.sourceImagesContainerView convertRect:sourceView.frame toView:self];
  145. UIImageView *tempView = [[UIImageView alloc] init];
  146. tempView.image = currentImageView.image;
  147. CGFloat h = (self.bounds.size.width / currentImageView.image.size.width) * currentImageView.image.size.height;
  148. if (!currentImageView.image) { // 防止 因imageview的image加载失败 导致 崩溃
  149. h = self.bounds.size.height;
  150. }
  151. tempView.bounds = CGRectMake(0, 0, self.bounds.size.width, h);
  152. tempView.center = self.center;
  153. [self addSubview:tempView];
  154. _saveButton.hidden = YES;
  155. [UIView animateWithDuration:SDPhotoBrowserHideImageAnimationDuration animations:^{
  156. tempView.frame = targetTemp;
  157. self.backgroundColor = [UIColor clearColor];
  158. } completion:^(BOOL finished) {
  159. [self removeFromSuperview];
  160. }];
  161. }
  162. - (void)layoutSubviews
  163. {
  164. [super layoutSubviews];
  165. CGRect rect = self.bounds;
  166. rect.size.width += SDPhotoBrowserImageViewMargin * 2;
  167. _scrollView.bounds = rect;
  168. _scrollView.center = self.center;
  169. CGFloat y = SDPhotoBrowserImageViewMargin;
  170. CGFloat w = _scrollView.frame.size.width - SDPhotoBrowserImageViewMargin * 2;
  171. CGFloat h = _scrollView.frame.size.height - SDPhotoBrowserImageViewMargin * 2;
  172. _scrollView.showsHorizontalScrollIndicator = NO;
  173. _scrollView.showsVerticalScrollIndicator = NO;
  174. [_scrollView.subviews enumerateObjectsUsingBlock:^(SDBrowserImageView *obj, NSUInteger idx, BOOL *stop) {
  175. CGFloat x = SDPhotoBrowserImageViewMargin + idx * (SDPhotoBrowserImageViewMargin * 2 + w);
  176. obj.frame = CGRectMake(x, y, w, h);
  177. }];
  178. _scrollView.contentSize = CGSizeMake(_scrollView.subviews.count * _scrollView.frame.size.width, 0);
  179. _scrollView.contentOffset = CGPointMake(self.currentImageIndex * _scrollView.frame.size.width, 0);
  180. _scrollView.pagingEnabled = YES;
  181. if (!_hasShowedFistView) {
  182. [self showFirstImage];
  183. }
  184. }
  185. - (void)show
  186. {
  187. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  188. self.frame = window.bounds;
  189. [window addSubview:self];
  190. }
  191. - (void)showFirstImage
  192. {
  193. UIView *sourceView = self.sourceImagesContainerView.subviews[self.currentImageIndex];
  194. CGRect rect = [self.sourceImagesContainerView convertRect:sourceView.frame toView:self];
  195. UIImageView *tempView = [[UIImageView alloc] init];
  196. tempView.image = [self placeholderImageForIndex:self.currentImageIndex];
  197. [self addSubview:tempView];
  198. CGRect targetTemp = [_scrollView.subviews[self.currentImageIndex] bounds];
  199. tempView.frame = rect;
  200. tempView.contentMode = [_scrollView.subviews[self.currentImageIndex] contentMode];
  201. _scrollView.hidden = YES;
  202. [UIView animateWithDuration:SDPhotoBrowserShowImageAnimationDuration animations:^{
  203. tempView.center = self.center;
  204. tempView.bounds = (CGRect){CGPointZero, targetTemp.size};
  205. } completion:^(BOOL finished) {
  206. _hasShowedFistView = YES;
  207. [tempView removeFromSuperview];
  208. _scrollView.hidden = NO;
  209. }];
  210. }
  211. - (UIImage *)placeholderImageForIndex:(NSInteger)index
  212. {
  213. if ([self.delegate respondsToSelector:@selector(photoBrowser:placeholderImageForIndex:)]) {
  214. return [self.delegate photoBrowser:self placeholderImageForIndex:index];
  215. }
  216. return nil;
  217. }
  218. - (NSURL *)highQualityImageURLForIndex:(NSInteger)index
  219. {
  220. if ([self.delegate respondsToSelector:@selector(photoBrowser:highQualityImageURLForIndex:)]) {
  221. return [self.delegate photoBrowser:self highQualityImageURLForIndex:index];
  222. }
  223. return nil;
  224. }
  225. #pragma mark - scrollview代理方法
  226. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  227. {
  228. int index = (scrollView.contentOffset.x + _scrollView.bounds.size.width * 0.5) / _scrollView.bounds.size.width;
  229. // 有过缩放的图片在拖动100后清除缩放
  230. CGFloat margin = 100.0;
  231. CGFloat x = scrollView.contentOffset.x;
  232. if ((x - index * self.bounds.size.width) > margin || (x - index * self.bounds.size.width) < - margin) {
  233. SDBrowserImageView *imageView = _scrollView.subviews[index];
  234. if (imageView.isScaled) {
  235. [UIView animateWithDuration:0.5 animations:^{
  236. imageView.transform = CGAffineTransformIdentity;
  237. } completion:^(BOOL finished) {
  238. [imageView eliminateScale];
  239. }];
  240. }
  241. }
  242. _indexLabel.text = [NSString stringWithFormat:@"%d/%ld", index + 1, (long)self.imageCount];
  243. [self setupImageOfImageViewForIndex:index];
  244. }
  245. @end
  246. // 版权属于原作者
  247. // http://code4app.com (cn) http://code4app.net (en)
  248. // 发布代码于最专业的源码分享网站: Code4App.com