|
|
@@ -0,0 +1,195 @@
|
|
|
+//
|
|
|
+// NewPhotoPreviewController.m
|
|
|
+// RA Image
|
|
|
+//
|
|
|
+// Created by Jack on 2017/6/14.
|
|
|
+// Copyright © 2017年 USAI. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+#import "NewPhotoPreviewController.h"
|
|
|
+#import "PhotoPreviewCell.h"
|
|
|
+
|
|
|
+@interface NewPhotoPreviewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
|
|
|
+
|
|
|
+@property (strong, nonatomic) IBOutlet UILabel *indicator;
|
|
|
+@property (strong, nonatomic) IBOutlet UICollectionView *previewContainer;
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation NewPhotoPreviewController
|
|
|
+
|
|
|
+- (void)viewDidLoad {
|
|
|
+ [super viewDidLoad];
|
|
|
+ // Do any additional setup after loading the view.
|
|
|
+// self.automaticallyAdjustsScrollViewInsets = NO;
|
|
|
+// self.navigationController.navigationBar.translucent = NO;
|
|
|
+// self.edgesForExtendedLayout = UIRectEdgeNone;
|
|
|
+
|
|
|
+ UIView *v = [UIView new];
|
|
|
+ [self.view insertSubview:v atIndex:0];
|
|
|
+
|
|
|
+ self.indicator.layer.cornerRadius = 20;
|
|
|
+ self.indicator.layer.masksToBounds = YES;
|
|
|
+
|
|
|
+ self.previewContainer.pagingEnabled = YES;
|
|
|
+ NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
|
|
|
+ self.indicator.text = offset;
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)viewDidLayoutSubviews {
|
|
|
+ [super viewDidLayoutSubviews];
|
|
|
+//
|
|
|
+// UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.previewContainer.collectionViewLayout;
|
|
|
+// CGFloat w = CGRectGetWidth(self.view.frame);
|
|
|
+// CGFloat h = CGRectGetHeight(self.view.frame);
|
|
|
+// layout.itemSize = CGSizeMake(w,h);
|
|
|
+// layout.minimumLineSpacing = 0;
|
|
|
+// layout.minimumInteritemSpacing = 0;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)viewDidAppear:(BOOL)animated {
|
|
|
+ [super viewDidAppear:animated];
|
|
|
+
|
|
|
+ [self.previewContainer reloadData]; // 重新布局Item大小才正确
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+- (void)didReceiveMemoryWarning {
|
|
|
+ [super didReceiveMemoryWarning];
|
|
|
+ // Dispose of any resources that can be recreated.
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Setter
|
|
|
+
|
|
|
+- (void)setCurrentIndex:(NSUInteger)currentIndex {
|
|
|
+ _currentIndex = currentIndex;
|
|
|
+ NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
|
|
|
+ self.indicator.text = offset;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - FlowLayout Delegate
|
|
|
+
|
|
|
+- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ return collectionView.bounds.size;
|
|
|
+}
|
|
|
+
|
|
|
+- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
|
|
|
+ return UIEdgeInsetsZero;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)collectionviewdidend
|
|
|
+
|
|
|
+#pragma mark - CollectionView Delegate
|
|
|
+
|
|
|
+- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
|
|
|
+
|
|
|
+ self.currentIndex = indexPath.row;
|
|
|
+
|
|
|
+ UIScrollView *sc = preCell.scrollView;
|
|
|
+ sc.delegate = self;
|
|
|
+ sc.maximumZoomScale = 5;
|
|
|
+ sc.showsVerticalScrollIndicator = NO;
|
|
|
+ sc.showsHorizontalScrollIndicator = NO;
|
|
|
+ NSDictionary *item = [self.photos objectAtIndex:indexPath.row];
|
|
|
+ UIImage *img = [UIImage imageWithContentsOfFile:[item objectForKey:@"path"]];
|
|
|
+
|
|
|
+ CGFloat width = CGRectGetWidth(cell.bounds);
|
|
|
+ CGFloat height = CGRectGetHeight(cell.bounds);
|
|
|
+ CGFloat w = img.size.width;
|
|
|
+ CGFloat h = img.size.height;
|
|
|
+ // 根据图片大小和ScrollView大小等比缩放,使ScrollView容得下图片
|
|
|
+ float factor = MAX(w / width, h / height);
|
|
|
+ factor = 1 / factor * 0.8;
|
|
|
+
|
|
|
+ CGRect frame = CGRectMake((width - w * factor) * 0.5, (height - h * factor) * 0.5, w * factor, h * factor);
|
|
|
+ UIImageView *iv = [[UIImageView alloc] initWithImage:img];
|
|
|
+ iv.frame = frame;
|
|
|
+ iv.userInteractionEnabled = YES;
|
|
|
+ for (UIView *v in sc.subviews) {
|
|
|
+ [v removeFromSuperview];
|
|
|
+ }
|
|
|
+ [sc addSubview:iv];
|
|
|
+ sc.contentSize = CGSizeZero;
|
|
|
+ sc.contentOffset = CGPointZero;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
|
|
|
+ UIScrollView *sc = preCell.scrollView;
|
|
|
+ for (UIView *v in sc.subviews) {
|
|
|
+ [v removeFromSuperview];
|
|
|
+ }
|
|
|
+ sc.zoomScale = 1;
|
|
|
+ sc.contentSize = CGSizeZero;
|
|
|
+ sc.contentOffset = CGPointZero;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+#pragma mark - CollectionView DataSource
|
|
|
+
|
|
|
+- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
|
|
+ return self.photos.count;
|
|
|
+}
|
|
|
+
|
|
|
+- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+
|
|
|
+ PhotoPreviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoPreviewCell" forIndexPath:indexPath];
|
|
|
+
|
|
|
+ return cell;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - ScrollView Delegate
|
|
|
+
|
|
|
+- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
|
|
|
+ if (scrollView != self.previewContainer) {
|
|
|
+ return scrollView.subviews.firstObject;
|
|
|
+ }
|
|
|
+ return nil;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
|
|
|
+ if (self.previewContainer != scrollView) {
|
|
|
+ CGFloat width = CGRectGetWidth(self.previewContainer.bounds);
|
|
|
+ CGFloat height = CGRectGetHeight(self.previewContainer.bounds);
|
|
|
+
|
|
|
+ CGSize itemSize = [self itemSize:self.currentIndex];
|
|
|
+ CGSize contentSize = scrollView.contentSize;
|
|
|
+ contentSize.width += (width - itemSize.width);
|
|
|
+ contentSize.height += (height - itemSize.height);
|
|
|
+ scrollView.contentSize = contentSize;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (CGSize)itemSize:(NSUInteger)index {
|
|
|
+
|
|
|
+ CGFloat width = CGRectGetWidth(self.previewContainer.bounds);
|
|
|
+ CGFloat height = CGRectGetHeight(self.previewContainer.bounds);
|
|
|
+ CGSize size = CGSizeMake(width, height);
|
|
|
+
|
|
|
+ NSDictionary *item = [self.photos objectAtIndex:index];
|
|
|
+ UIImage *img = [UIImage imageWithContentsOfFile:[item objectForKey:@"path"]];
|
|
|
+ CGFloat w = img.size.width;
|
|
|
+ CGFloat h = img.size.height;
|
|
|
+ // 根据图片大小和ScrollView大小等比缩放,使ScrollView容得下图片
|
|
|
+ float factor = MAX(w / width, h / height);
|
|
|
+ factor = 1 / factor * 0.8;
|
|
|
+
|
|
|
+ size = CGSizeMake(w * factor, h * factor);
|
|
|
+ return size;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+@end
|