ios 图片预览器

版权声明:禁止转载、复制 https://blog.csdn.net/qq_37191821/article/details/85090740

思路:使用UICollectionView显示图片,点击查看大图,代码如下:

#import <UIKit/UIKit.h>

/** 九宫格排列图片*/

@interface ZJPhotoAlumView : UIView

/** 展示图片

 *  @param imageStrs 可以是本地图片名称也可是网络图片地址 */

- (void)showImagesWithImageStrs:(NSArray<NSString*>*)imageStrs;

/** 是否在末尾展示添加按钮*/

- (void)showPhotoAlumAddImage:(BOOL)show clickBlock:(void(^)(void))block;

/** 大图展示时是否显示删除按钮

 *  @param block 回调删除后的集合和当前显示第一张*/

- (void)showDeleteBtnBackBlock:(void(^)(NSArray <NSString*>*imageStrs,NSInteger currentIndex))block;

@end

/** 全屏查看图片原图 */

@interface ZJOriginalImageView : UIView

/** 展示大图

 *  @param imageStrs 可以是本地图片名称也可是网络图片地址

 *  @param currentIndex 默认显示第几张图*/

- (void)showImagesWithImageStrs:(NSArray<NSString*>*)imageStrs defaultCurrentIndex:(NSInteger)currentIndex;

/** 显示删除按钮

 *  @param block 回调删除后的集合和当前显示第一张*/

- (void)showDeleteBtnBackBlock:(void(^)(NSArray <NSString*>*imageStrs,NSInteger currentIndex))block;

/** 设置当前显示图片的frame */

- (void)setDefaultCurrentIndexSubViewFrame:(CGRect)frame;

/** 点击当前图片的回调 */

- (void)didSelectSubviewAtIndex:(void(^)(NSInteger index))block;

@end

#import "ZJPhotoAlumView.h"

@class ZJOriginalImageView;

// 边距

#define KZJPhotoAlumSubMargin 5

// 每一行排列的个数

#define KZJPhotoAlumColumns 3

// 动画时间

#define KZJPhotoAlumAnimateTime 0.3

@interface ZJPhotoAlumView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,copy) void(^viewSubViewClickBlock)(id model,NSInteger tag);

@property(nonatomic,weak) UICollectionView * collect;

@property(nonatomic,strong) NSMutableArray <NSString*>* dataArray;

@property(nonatomic,strong) ZJOriginalImageView   * originalImageView;

@property(nonatomic,assign) BOOL  showAddImage;

@property(nonatomic,copy)   void(^addImageClickBlock)(void);

@end

@implementation ZJPhotoAlumView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        

        [self setShowAddImage:false];

        [self setSubViews];

        

        [self handleSelfHeightWithImageStrs];

        

        [self handleOriginalImagesBlock];

    }

    return self;

}

-(void)setFrame:(CGRect)frame{

    [super setFrame:frame];

    CGRect newframe = [[self collect] frame];

    newframe.size = frame.size;

    [[self collect] setFrame:newframe];

}

-(void)loadData{

    [self setDataArray:[NSMutableArray array]];

    [[self dataArray] addObject:@"好像没有网络~"];

    [[self dataArray] addObject:@"空订单"];

    [[self dataArray] addObject:@"空购物车"];

    [[self dataArray] addObject:@"空消息"];

    [[self dataArray] addObject:@"guide_icon01"];

    [[self dataArray] addObject:@"guide_icon02"];

    [[self dataArray] addObject:@"guide_icon03"];

    [[self dataArray] addObject:@"安全"];

    [[self dataArray] addObject:@"报警"];

    [[self dataArray] addObject:@"更多"];

}

-(ZJOriginalImageView *)originalImageView{

    if(!_originalImageView){

        CGSize size = [[UIScreen mainScreen] bounds].size;

        CGRect subframe = CGRectMake(0, 0, size.width, size.height);

        _originalImageView = [[ZJOriginalImageView alloc] initWithFrame:subframe];

        [_originalImageView setBackgroundColor:[UIColor blackColor]];

    }

    return _originalImageView;

}

-(void)setSubViews{

    CGRect subframe = CGRectMake(0, 0, CGRectGetWidth([self frame]), CGRectGetHeight([self frame]));

    UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new];

    [layout setMinimumInteritemSpacing:KZJPhotoAlumSubMargin*0.8];

    [layout setMinimumLineSpacing:KZJPhotoAlumSubMargin];

    UICollectionView * subview = [[UICollectionView alloc] initWithFrame:subframe collectionViewLayout:layout];

    [subview setDataSource:self];

    [subview setDelegate:self];

    [subview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];

    [subview setBackgroundColor:[UIColor whiteColor]];

    [self addSubview:subview];

    [self setCollect:subview];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    

    return [self showAddImage] == true ? [[self dataArray] count]+1 : [[self dataArray] count];

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    if([cell viewWithTag:101] == nil){

        CGRect subframe   = CGRectMake(0, 0, CGRectGetWidth([cell frame]), CGRectGetHeight([cell frame]));

        UIImageView * subview = [[UIImageView alloc] initWithFrame:subframe];

        [subview setTag:101];

        [cell addSubview:subview];

    }

    UIImageView * subview = (UIImageView *)[cell viewWithTag:101];

    if([indexPath item]<[[self dataArray]count]){

        [self imageView:subview imageString:[[self dataArray] objectAtIndex:[indexPath item]]];

    }else{

        [subview setImage:[UIImage imageNamed:@"b"]];

    }

    return cell;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    if([indexPath item]<[[self dataArray]count]){

        [self showOriginalImagesWithCurrentIndex:[indexPath item]];

    }else if([self addImageClickBlock]){

        self.addImageClickBlock();

    }

}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(KZJPhotoAlumSubMargin, KZJPhotoAlumSubMargin, KZJPhotoAlumSubMargin, KZJPhotoAlumSubMargin);

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    CGFloat subWidth = (CGRectGetWidth([self frame])-(KZJPhotoAlumColumns+1)*KZJPhotoAlumSubMargin)*1.0/KZJPhotoAlumColumns;

    return CGSizeMake(subWidth, subWidth);

}

#pragma mark  ********************************************

-(void)imageView:(UIImageView*)imgView imageString:(NSString*)string{

    if([UIImage imageNamed:string]){ // 本地图片名称

        [imgView setImage:[UIImage imageNamed:string]];

    }else if([UIImage imageWithContentsOfFile:string]){// 本地图片路径

        [imgView setImage:[UIImage imageWithContentsOfFile:string]];

    }else{ // 网络图片

        

    }

}

-(void)handleSelfHeightWithImageStrs{

    NSInteger count = [self showAddImage]==true?[[self dataArray]count]+1:[[self dataArray]count];

    CGFloat subWidth = (CGRectGetWidth([self frame])-(KZJPhotoAlumColumns+1)*KZJPhotoAlumSubMargin)*1.0/KZJPhotoAlumColumns;

    NSInteger row = count%KZJPhotoAlumColumns==0?count/KZJPhotoAlumColumns:(count/KZJPhotoAlumColumns+1);

    CGFloat height = (subWidth+KZJPhotoAlumSubMargin)*row+KZJPhotoAlumSubMargin;

    CGRect newFrame = [self frame];

    newFrame.size.height = height;

    [self setFrame:newFrame];

}

#pragma mark  获取点击图片的frame

- (CGRect)getConverFrameWithCurrentIndex:(NSInteger)index{

    NSIndexPath * idnexPath = [NSIndexPath indexPathForItem:index inSection:0];

    UICollectionViewCell * cell = [[self collect] cellForItemAtIndexPath:idnexPath];

    CGRect newFrame = [self convertRect:[cell frame] fromView:[cell superview]];

    return  newFrame;

}

-(void)showOriginalImagesWithCurrentIndex:(NSInteger)currentIndex{

    CGRect oldFrame = [self getConverFrameWithCurrentIndex:currentIndex];

    [[self originalImageView] showImagesWithImageStrs:[self dataArray] defaultCurrentIndex:currentIndex];

    [[self originalImageView] setDefaultCurrentIndexSubViewFrame:oldFrame];

    [[UIApplication sharedApplication].keyWindow addSubview:[self originalImageView]];

    CGSize size = [[UIScreen mainScreen] bounds].size;

    CGRect newFrame = CGRectMake(0, 0, size.width, size.height);

    [UIView animateWithDuration:KZJPhotoAlumAnimateTime animations:^{

        [[self originalImageView] setAlpha:1];

        [[self originalImageView] setDefaultCurrentIndexSubViewFrame:newFrame];

    }];

}

-(void)handleOriginalImagesBlock{

    __weak typeof(self) weakSelf = self;

    [[self originalImageView] didSelectSubviewAtIndex:^(NSInteger index) {

        CGRect cellFrame = [weakSelf getConverFrameWithCurrentIndex:index];

        [UIView animateWithDuration:KZJPhotoAlumAnimateTime animations:^{

            [[weakSelf originalImageView] setAlpha:0];

            [[weakSelf originalImageView] setDefaultCurrentIndexSubViewFrame:cellFrame];

        } completion:^(BOOL finished) {

            [[weakSelf originalImageView] removeFromSuperview];

        }];

    }];

    [[self originalImageView] showDeleteBtnBackBlock:^(NSArray<NSString *> *imageStrs, NSInteger currentIndex) {

        [weakSelf setDataArray:[NSMutableArray arrayWithArray:imageStrs]];

        [weakSelf handleSelfHeightWithImageStrs];

        [[weakSelf collect] reloadData];

    }];

}

#pragma mark ****************************************************

- (void)showImagesWithImageStrs:(NSArray<NSString*>*)imageStrs{

    [self setDataArray:[NSMutableArray arrayWithArray:imageStrs]];

    [self handleSelfHeightWithImageStrs];

    [[self collect] reloadData];

}

- (void)showPhotoAlumAddImage:(BOOL)show clickBlock:(void(^)(void))block{

    [self setShowAddImage:show];

    [self setAddImageClickBlock:block];

    [[self collect] reloadData];

}

- (void)showDeleteBtnBackBlock:(void(^)(NSArray <NSString*>*imageStrs,NSInteger currentIndex))block{

    [[self originalImageView] showDeleteBtnBackBlock:block];

}

@end

#pragma mark - >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

@interface ZJOriginalImageView ()<UIScrollViewDelegate>

@property(nonatomic,weak) UILabel  * countMsgLabel;

@property(nonatomic,weak) UIButton * deleteBtn;

@property(nonatomic,strong) NSMutableArray <NSString*>* imageStrs;

@property(nonatomic,weak) UIScrollView * scrollview;

@property(nonatomic,assign) NSInteger    currentIndex;

@property(nonatomic,weak) UIImageView  * leftImgView;

@property(nonatomic,weak) UIImageView  * centerImgView;

@property(nonatomic,weak) UIImageView  * rightImgView;

@property(nonatomic,copy) void(^didSelectSubviewAtIndexBlock)(NSInteger index);

@property(nonatomic,copy) void(^deleteBackBlock)(NSArray * array, NSInteger index);

@end

@implementation ZJOriginalImageView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        [self setSubViews];

    }

    return self;

}

-(void)setSubViews{

    CGRect subframe = CGRectMake(0, 0, CGRectGetWidth([self frame]), CGRectGetHeight([self frame]));

    UIScrollView * scroll = [[UIScrollView alloc] initWithFrame:subframe];

    [scroll setBounces:true];

    [scroll setPagingEnabled:true];

    [scroll setShowsVerticalScrollIndicator:false];

    [scroll setShowsHorizontalScrollIndicator:false];

    [self addSubview:scroll];

    [self setScrollview:scroll];

    

    for(int i=0;i<3;i++){

        CGRect subframe = CGRectMake(CGRectGetWidth([scroll frame])*i, 0, CGRectGetWidth([scroll frame]), CGRectGetHeight([scroll frame]));

        UIImageView * subview01 = [[UIImageView alloc] initWithFrame:subframe];

        [subview01 setContentMode:UIViewContentModeScaleAspectFit];

        //[subview01 setBackgroundColor:[UIColor redColor]];

        [scroll addSubview:subview01];

        if(i==0){[self setLeftImgView:subview01];}

        if(i==1){[self setCenterImgView:subview01];}

        if(i==2){[self setRightImgView:subview01];}

    }

    subframe = CGRectMake(0, 0, CGRectGetWidth([self frame]), 44.0);

    UILabel * label = [[UILabel alloc] initWithFrame:subframe];

    [label setFont:[UIFont systemFontOfSize:14]];

    [label setTextAlignment:NSTextAlignmentCenter];

    [label setBackgroundColor:[UIColor clearColor]];

    [label setTextColor:[UIColor whiteColor]];

    [self addSubview:label];

    [self setCountMsgLabel:label];

    

    subframe = CGRectMake(CGRectGetWidth([self frame])-64.0, 10, 44.0, 44.0);

    UIButton * button = [[UIButton alloc] initWithFrame:subframe];

    //[button setBackgroundColor:[UIColor orangeColor]];

    [button addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];

    [button setTitle:@"删除" forState:UIControlStateNormal];

    [[button titleLabel] setFont:[UIFont systemFontOfSize:14]];

    [button setHidden:true];

    [self addSubview:button];

    [self setDeleteBtn:button];

    

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];

    [self addGestureRecognizer:tap];

}

-(void)deleteButtonClick{

    if([[self imageStrs] count]==1){

        [[self imageStrs] removeObjectAtIndex:0];

        if([self deleteBackBlock]){

            self.deleteBackBlock([self imageStrs], 0);

        }

        [self tapClick];

    }else{

        [[self imageStrs] removeObjectAtIndex:[self currentIndex]];

        if([self currentIndex]>=[[self imageStrs] count]){

            [self setCurrentIndex:[[self imageStrs] count]-1];

        }

        [self setDefaultCurrentIndex:[self currentIndex]];

        if([self deleteBackBlock]){

            self.deleteBackBlock([self imageStrs], [self currentIndex]);

        }

    }

}

-(void)tapClick{

    if([self didSelectSubviewAtIndexBlock]){

        self.didSelectSubviewAtIndexBlock([self currentIndex]);

    }

}

-(void)refreshScrollImagesLayoutWithImageStrs:(NSArray*)imageStrs{

    if([imageStrs count]==0){

        [[self scrollview] setDelegate:nil];

    }else if([imageStrs count] ==1){

        [[self scrollview] setDelegate:nil];

        [self imageView:[self leftImgView] imageString:[imageStrs firstObject]];

    }else{

        [[self scrollview] setDelegate:self];

        [[self scrollview] setContentSize:CGSizeMake([self sw]*3, 0)];

        [[self scrollview] setContentOffset:CGPointMake([self sw], 0)];

    }

}

#pragma mark  *******************************************************

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    [self refrshScrollImageViewAndCurrentIndex];

}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

    [self refrshScrollImageViewAndCurrentIndex];

}

-(void)refrshScrollImageViewAndCurrentIndex{

    // 计算索引

    CGFloat offetIndex = [[self scrollview] contentOffset].x/CGRectGetWidth([[self scrollview] frame]);

    if(offetIndex == 1){return;}

    if(offetIndex>1){// 右往左滑+1

        [self setCurrentIndex:[self currentIndex]+1];

        if([self currentIndex]>=[[self imageStrs] count]){

            // 确保索引范围

            [self setCurrentIndex:0];

        }

    }else if(offetIndex<1){// 左往右滑-1

        [self setCurrentIndex:[self currentIndex]-1];

        if([self currentIndex] <= -1){

            // 确保索引范围

            [self setCurrentIndex:[[self imageStrs] count]-1];

        }

    }

    // 更新UIImage

    [self handleLeftImageViewWithIndex:[self currentIndex]-1];

    [self handleCenterImageViewWithIndex:[self currentIndex]];

    [self handleRightImageViewWithIndex:[self currentIndex]+1];

    // 保证中心位置

    [[self scrollview] setContentOffset:CGPointMake(CGRectGetWidth([[self scrollview] frame]), 0)];

}

-(void)handleLeftImageViewWithIndex:(NSInteger)index{

    if(index<= -1){

        [self imageView:[self leftImgView] imageString:[[self imageStrs] lastObject]];

    }else if(index<=[[self imageStrs] count]-1){

        [self imageView:[self leftImgView] imageString:[[self imageStrs] objectAtIndex:index]];

    }

}

-(void)handleCenterImageViewWithIndex:(NSInteger)index{

    if(index<[[self imageStrs] count]){

        [self imageView:[self centerImgView] imageString:[[self imageStrs] objectAtIndex:index]];

    }

}

-(void)handleRightImageViewWithIndex:(NSInteger)index{

    if(index>=[[self imageStrs] count]){

        [self imageView:[self rightImgView] imageString:[[self imageStrs] firstObject]];

    }else if(index>=0){

        [self imageView:[self rightImgView] imageString:[[self imageStrs] objectAtIndex:index]];

    }

}

-(void)imageView:(UIImageView*)imgView imageString:(NSString*)string{

    if([UIImage imageNamed:string]){ // 本地图片名称

        [imgView setImage:[UIImage imageNamed:string]];

    }else if([UIImage imageWithContentsOfFile:string]){// 本地图片路径

        [imgView setImage:[UIImage imageWithContentsOfFile:string]];

    }else{ // 网络图片

        

    }

}

#pragma mark  *******************************************************

- (void)showImagesWithImageStrs:(NSArray<NSString*>*)imageStrs defaultCurrentIndex:(NSInteger)currentIndex{

    [self setImageStrs:[NSMutableArray arrayWithArray:imageStrs]];

    [self refreshScrollImagesLayoutWithImageStrs:imageStrs];

    [self setDefaultCurrentIndex:currentIndex];

}

- (void)showDeleteBtnBackBlock:(void(^)(NSArray <NSString*>*imageStrs,NSInteger currentIndex))block;{

    [[self deleteBtn] setHidden:false];

    [self setDeleteBackBlock:block];

}

- (void)setDefaultCurrentIndex:(NSInteger)currentIndex{

    [self setCurrentIndex:currentIndex];

    // 更新UIImage

    [self handleLeftImageViewWithIndex:[self currentIndex]-1];

    [self handleCenterImageViewWithIndex:[self currentIndex]];

    [self handleRightImageViewWithIndex:[self currentIndex]+1];

}

- (void)setDefaultCurrentIndexSubViewFrame:(CGRect)frame{

    CGRect newFrame = frame;

    newFrame.origin.x += [self sw];

    [[self centerImgView] setFrame:newFrame];

}

- (void)didSelectSubviewAtIndex:(void(^)(NSInteger index))block{

    [self setDidSelectSubviewAtIndexBlock:block];

}

@end

猜你喜欢

转载自blog.csdn.net/qq_37191821/article/details/85090740
今日推荐