iOS仿探探六宫格相册的实现,带添加和删除功能

先看一下实现效果如下图:
仿探探的九宫格相册截图
具体实现代码如下:
View视图的创建

#import <UIKit/UIKit.h>
@protocol PhotoViewDelegate <NSObject>
-(void)addPhotoLessThanMax:(NSInteger)num;//添加图片
-(void)deletePhotoTap:(NSInteger)tag;//删除图片
@end
typedef void (^PhotoBlock)(BOOL isBool);
@interface PhotoView : UIView<UIGestureRecognizerDelegate>
@property (assign, nonatomic) id <PhotoViewDelegate>delegate;
@property (strong, nonatomic)NSMutableArray *imgArray;
@property(nonatomic,copy)PhotoBlock inputBlock;
- (instancetype)initWithFrame:(CGRect)frame;
@end



#import "PhotoView.h"
#import "UIImage+GIF.h"
#define BIGIMAGE_WIDTH (ScreenWidth-70)/3*2+10
#define DIVIDE_WIDTH 10
#define SMALLIMAGE_WIDTH (ScreenWidth-70)/3

#define MOVEIMAGE_WIDTH SMALLIMAGE_WIDTH * 10

@interface PhotoView()
@property (strong, nonatomic)UIImageView *moveImageView;
@property (nonatomic,strong)NSMutableArray *viewArray;
@property (nonatomic,strong)NSMutableArray * btnAray;
@property (nonatomic,strong)NSMutableArray *rectArray;
@property (nonatomic,assign)BOOL isLongPress;
@property (nonatomic,assign)float touchX;
@property (nonatomic,assign)float touchY;
@property (nonatomic,assign)CGRect moveInitFrame;
@property (nonatomic,assign)CGRect moveFinishRect;
@property (nonatomic,assign)int startPosition;
@property (nonatomic,assign)BOOL isChangeEnd;
@property (nonatomic,assign)BOOL isPerfermMove;
@end

@implementation PhotoView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initRect];
        [self initShowImageView];
    }
    return self;
}

-(void)setImgArray:(NSMutableArray *)imgArray{
    _imgArray = imgArray;
    [self showImage];
}
-(void)initRect{
    NSValue *value0 = [NSValue valueWithCGRect:CGRectMake(0, 0, BIGIMAGE_WIDTH, BIGIMAGE_WIDTH)];
    NSValue *value1 = [NSValue valueWithCGRect:CGRectMake(BIGIMAGE_WIDTH + DIVIDE_WIDTH, 0, SMALLIMAGE_WIDTH, SMALLIMAGE_WIDTH)];
    NSValue *value2 = [NSValue valueWithCGRect:CGRectMake(BIGIMAGE_WIDTH + DIVIDE_WIDTH, SMALLIMAGE_WIDTH + DIVIDE_WIDTH, SMALLIMAGE_WIDTH, SMALLIMAGE_WIDTH)];
    NSValue *value3 = [NSValue valueWithCGRect:CGRectMake(0, BIGIMAGE_WIDTH + DIVIDE_WIDTH, SMALLIMAGE_WIDTH, SMALLIMAGE_WIDTH)];
    NSValue *value4 = [NSValue valueWithCGRect:CGRectMake(SMALLIMAGE_WIDTH + DIVIDE_WIDTH, BIGIMAGE_WIDTH + DIVIDE_WIDTH, SMALLIMAGE_WIDTH, SMALLIMAGE_WIDTH)];
    NSValue *value5 = [NSValue valueWithCGRect:CGRectMake(BIGIMAGE_WIDTH + DIVIDE_WIDTH, 2 * (SMALLIMAGE_WIDTH + DIVIDE_WIDTH), SMALLIMAGE_WIDTH, SMALLIMAGE_WIDTH)];
    self.rectArray = [NSMutableArray arrayWithObjects:value0,value1,value2,value3,value4,value5,nil];
}
//每次编辑之后重新排列图片,更新新添加的图片
-(void)showImage{
    if (self.inputBlock) {
        if (_imgArray.count >0) {
            self.inputBlock(YES);
        }else{
            self.inputBlock(NO);
        }
    }
    for (UIImageView * imageView in self.viewArray) {
        UIButton * btn= self.btnAray[imageView.tag];
        UIImage *image;
        if (_imgArray.count == 0) {
            imageView.contentMode = UIViewContentModeCenter;
            image = [UIImage imageNamed:@"添加图片"];
            UITapGestureRecognizer *recongnizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleAddTapPress:)];
            [imageView addGestureRecognizer:recongnizer];
            btn.hidden = YES;
            imageView.image = image;
            return;
        }
        if (imageView.tag > _imgArray.count-1) {
            btn.hidden = YES;
            image = [UIImage imageNamed:@"添加图片"];
            imageView.contentMode = UIViewContentModeCenter;
            UITapGestureRecognizer *recongnizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleAddTapPress:)];
            [imageView addGestureRecognizer:recongnizer];
        }else{
            image  = [_imgArray objectAtIndex:imageView.tag];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            btn.hidden = NO;
        }
        imageView.image = image;
    }
}
-(void)initShowImageView{
    self.viewArray = [NSMutableArray array];
    self.btnAray = [NSMutableArray array];
    for(int i= 0 ; i < self.rectArray.count ; i++){
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.backgroundColor = C6_238;
        imageView.tag = i;
        imageView.userInteractionEnabled = YES;
        imageView.layer.masksToBounds = YES;
        imageView.layer.cornerRadius = 10;
        imageView.frame = [[self.rectArray objectAtIndex:i] CGRectValue];
        [self.viewArray addObject:imageView];
        [self addSubview:imageView];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(imageView.width-30, imageView.height-30, 25, 25);
        [btn setBackgroundImage:[UIImage imageNamed:@"删除照片"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(handleDeleteTapPress:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = imageView.tag;
        btn.hidden = YES;
        [self.btnAray addObject:btn];
        [imageView addSubview:btn];
    }
}
#pragma mark ------添加照片----
-(void)handleAddTapPress:(UITapGestureRecognizer *)sender{
     if(self.delegate){
        [self.delegate addPhotoLessThanMax:6-self.imgArray.count];
    }
}
#pragma mark ---删除图片-----
-(void)handleDeleteTapPress:(UIButton *)sender{
    if(self.delegate){
        [self.delegate deletePhotoTap:sender.tag];
    }
    [self.imgArray removeObjectAtIndex:sender.tag];
    [self showImage];
}
@end

controller里的实现

controller里选取照片后的回调方法中把所选择的新的图片以UIIMage的形式添加进photoView的图片数组里

 //photos为新增加的图片数组
 NSMutableArray * phot = [NSMutableArray array];
 phot = weakSelf.photoView.imgArray;
 [phot  addObjectsFromArray:photos];
 //把新图片数组重新赋值给photoView
 weakSelf.photoView.imgArray = phot;

controller里实现photoView的代理方法,实现删除和增加功能后,图片的增减问题,num为最多还可选几张图片六宫格最多显示只能显示6张,num=6-已有图片数。保存编辑图片信息的时候需要把最终新添加的图片数组bigImageDataArray里所存的图片DATA的形式转换成URL的形式,与编辑后的储存URL的数组imageUrlDicArray合并一起,然后上传至服务器端。

#pragma mark ------图片编辑代理方法
-(void)addPhotoLessThanMax:(NSInteger)num{
    //点击加号调取进入相册选图片方法
    [self pushImagePickerController:num];
}
-(void)deletePhotoTap:(NSInteger)tag{
    if (tag<self.imageUrlDicArray.count) {
    //说明删除的是线上已有的图片
        [self.imageUrlDicArray removeObjectAtIndex:tag];
    }else{
        //说明删除的是新添加的图片
        [self.bigImageDataArray removeObjectAtIndex:tag];
    }
}

好啦,这个页面就写到这里,稍后有空会添加图片的选择上传以及压缩~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章