JCHATPhotoBrowserCollectionViewCell.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // HMPhotoBrowserCollectionViewCell.m
  3. // photosFramework
  4. //
  5. // Created by HuminiOS on 15/11/12.
  6. // Copyright © 2015年 HuminiOS. All rights reserved.
  7. //
  8. #import "JCHATPhotoBrowserCollectionViewCell.h"
  9. @interface JCHATPhotoBrowserCollectionViewCell ()<UIScrollViewDelegate>
  10. @property (strong, nonatomic)JCHATPhotoModel *photoModel;
  11. @property (strong, nonatomic)UIImageView *largeImage;
  12. @end
  13. @implementation JCHATPhotoBrowserCollectionViewCell
  14. - (void)awakeFromNib {
  15. [super awakeFromNib];
  16. // Initialization code
  17. _largeImage = [UIImageView new];
  18. _largeImage.contentMode = UIViewContentModeScaleAspectFit;
  19. [_imageContent addSubview:_largeImage];
  20. self.backgroundColor = [UIColor blackColor];
  21. self.userInteractionEnabled = YES;
  22. _imageContent.userInteractionEnabled = YES;
  23. _imageContent.delegate = self;
  24. _imageContent.maximumZoomScale = 2.0;
  25. _imageContent.minimumZoomScale = 1;
  26. _imageContent.decelerationRate = UIScrollViewDecelerationRateFast;
  27. _imageContent.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  28. _imageContent.scrollEnabled = YES;
  29. _largeImage.userInteractionEnabled = YES;
  30. // [self addGestureRecognizer:[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinAction:)]];
  31. //
  32. // [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]];
  33. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPhotoAction:)];
  34. tap.numberOfTapsRequired = 2;
  35. [_imageContent addGestureRecognizer:tap];
  36. }
  37. - (void)pinAction:(UIPinchGestureRecognizer *)pin{
  38. // if ((pin.state == UIGestureRecognizerStateEnded
  39. // || pin.state == UIGestureRecognizerStateCancelled
  40. // || pin.state == UIGestureRecognizerStateFailed)
  41. // && _largeImage.width < self.width){
  42. // [UIView animateWithDuration:0.2 animations:^{
  43. // _largeImage.transform = CGAffineTransformIdentity;
  44. // _imageContent.contentSize = _largeImage.frame.size;
  45. // self.photo_imageView.x = 0;
  46. // self.photo_imageView.y = 0;
  47. // self.bgScrollView.contentOffset = CGPointZero;
  48. // }];
  49. // return;
  50. // }else if (pin.state == UIGestureRecognizerStateChanged){
  51. // if (self.animating) return;
  52. //
  53. // self.photo_imageView.transform = CGAffineTransformScale(self.photo_imageView.transform, pin.scale, pin.scale);
  54. // self.bgScrollView.contentSize = self.photo_imageView.frame.size;
  55. //
  56. // self.bgScrollView.contentOffset = CGPointMake(self.bgScrollView.contentOffset.x - self.photo_imageView.x , self.bgScrollView.contentOffset.y - self.photo_imageView.y);
  57. //
  58. // self.photo_imageView.centerX = self.bgScrollView.contentOffset.x + self.bgScrollView.width / 2.0;
  59. // self.photo_imageView.centerY = self.bgScrollView.contentOffset.y + self.bgScrollView.height / 2.0;
  60. // CGPoint point = [pin locationInView:pin.view];
  61. // CGFloat scale = self.photo_imageView.width / self.bgScrollView.width;
  62. // if (scale > 3 || scale < 0.5){
  63. // self.animating = YES;
  64. // CGFloat reScale = scale > 1 ? 1.5 : 0.8 ;
  65. // [UIView animateWithDuration:0.5 animations:^{
  66. // self.photo_imageView.transform = CGAffineTransformScale(self.photo_imageView.transform, 1.0 / scale * reScale, 1.0 / scale * reScale );
  67. // self.bgScrollView.contentSize = self.photo_imageView.frame.size;
  68. //
  69. // self.bgScrollView.contentOffset = CGPointMake(self.bgScrollView.contentOffset.x - self.photo_imageView.x , self.bgScrollView.contentOffset.y - self.photo_imageView.y);
  70. //
  71. // self.photo_imageView.centerX = self.bgScrollView.contentOffset.x + self.bgScrollView.width / 2.0;
  72. // self.photo_imageView.centerY = self.bgScrollView.contentOffset.y + self.bgScrollView.height / 2.0;
  73. // }completion:^(BOOL finished) {
  74. // self.animating = NO;
  75. // }];
  76. // }else{
  77. //
  78. // }
  79. // }
  80. //
  81. // pin.scale = 1.0;
  82. }
  83. - (void)tapAction:(UITapGestureRecognizer *)tap{
  84. }
  85. - (void)tapPhotoAction:(UITapGestureRecognizer *)tap{
  86. [UIView animateWithDuration:0.4 animations:^{
  87. _largeImage.transform = CGAffineTransformIdentity;
  88. _imageContent.contentSize = _largeImage.frame.size;
  89. _imageContent.contentOffset = CGPointZero;
  90. }];
  91. }
  92. - (void)setDataWithModel:(JCHATPhotoModel *)model {
  93. _photoModel = model;
  94. PHAsset *asset = model.photoAsset;
  95. _imageContent.frame = self.bounds;
  96. _imageContent.contentSize = self.frame.size;
  97. _largeImage.frame = _imageContent.bounds;
  98. _largeImage.transform = CGAffineTransformIdentity;
  99. if (model.asset == nil) {
  100. CGFloat scale = [UIScreen mainScreen].scale;
  101. CGSize imageSize = CGSizeMake(self.frame.size.width * scale, self.frame.size.width * scale);
  102. [model.cachingManager requestImageForAsset:asset
  103. targetSize:imageSize
  104. contentMode:PHImageContentModeAspectFill
  105. options:nil
  106. resultHandler:^(UIImage *result, NSDictionary *info) {
  107. if ([_photoModel.photoAsset.localIdentifier isEqualToString:asset.localIdentifier]) {
  108. _largeImage.image = result;
  109. }
  110. }];
  111. } else {
  112. [[[ALAssetsLibrary alloc] init] assetForURL:model.imgURL resultBlock:^(ALAsset *asset) {
  113. _largeImage.image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
  114. }failureBlock:^(NSError *error) {
  115. NSLog(@"error=%@",error);
  116. }];
  117. }
  118. }
  119. #pragma mark - scrollViewDelegate
  120. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  121. return _largeImage;
  122. }
  123. - (void)scrollViewDidZoom:(UIScrollView *)scrollView
  124. {
  125. CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
  126. (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
  127. CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
  128. (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
  129. _largeImage.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
  130. scrollView.contentSize.height * 0.5 + offsetY);
  131. }
  132. @end