整体思路
- 创建两个可变数组,一个放网络图片链接.一个放本地图片对象.
- 图片展示的方式是如果有网络图片则先加载网络图片,没有的话从本地选择图片并进行展示.
- 每个cell上都有一个删除按钮.当reloadData的时候判断如果是最后一个cell则最后一个cell的删除按钮隐藏并且图片 = nil
- 点击删除按钮根据删除按钮的tag判断点击的cell是网络图片的 cell还是本地图片的cell,如果是网络图片cell,则删除网络图片链接的可变数组中的相应的链接并调用删除服务器数据库中的数据,并刷新collectionView,如果是本地图片则删除本地数组中图片对象并刷新即可.
代码如下
#import "ViewController.h"
#import "HBDefine.h"
#import "UIImageView+WebCache.h"
#import "SDWebImageDownloader.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property(nonatomic,strong) UICollectionView * collectionView;
@property(nonatomic,strong) UIImageView * imageView;
/** 网络图片链接数组 */
@property (nonatomic, strong) NSMutableArray *arrInternet;
/** 网络下载的图片 */
@property (nonatomic, strong) NSMutableArray *arrInternetPics;
/** 本地图片数组 */
@property (nonatomic, strong) NSMutableArray *arrLocal;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setUI];
self.arrInternet = [NSMutableArray arrayWithArray:@[@"http://img1.3lian.com/2015/w7/85/d/102.jpg",@"http://www.ytxww.com/upload/image/201203/20120309100442708165.jpg",@"http://imgsrc.baidu.com/forum/w%3D580/sign=79520e92632762d0803ea4b790ed0849/8a6104a4462309f740ec1ca3720e0cf3d6cad6a8.jpg"]];
}
//百度随便找的网络图片
//http://img1.3lian.com/2015/w7/85/d/102.jpg
//http://www.ytxww.com/upload/image/201203/20120309100442708165.jpg
//http://imgsrc.baidu.com/forum/w%3D580/sign=79520e92632762d0803ea4b790ed0849/8a6104a4462309f740ec1ca3720e0cf3d6cad6a8.jpg
#pragma mark - 一下是懒加载
-(NSMutableArray *)arrLocal{
if (_arrLocal == nil) {
_arrLocal = [NSMutableArray array];
}
return _arrLocal;
}
-(NSMutableArray *)arrInternetPics{
if (_arrInternetPics == nil) {
_arrInternetPics = [NSMutableArray array];
}
return _arrInternetPics;
}
-(NSMutableArray *)arrInternet{
if (_arrInternet == nil) {
_arrInternet = [NSMutableArray array];
}
return _arrInternet;
}
-(void)setUI{
CGFloat margin = 10;
CGFloat width = (KScreen_Width - margin * 5) / 5;
UICollectionViewFlowLayout * flowLayout = [UICollectionViewFlowLayout new];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
flowLayout.itemSize = CGSizeMake(width, width);
flowLayout.minimumLineSpacing = 10;
// flowLayout.minimumInteritemSpacing = 0;
flowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);
UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:KFrame(0, 100, KScreen_Width, 200) collectionViewLayout:flowLayout];
self.collectionView = collectionView;
[self.view addSubview:collectionView];
collectionView.backgroundColor = [UIColor orangeColor];
collectionView.delegate = self;
collectionView.dataSource = self;
[collectionView registerClass:[PBCell class] forCellWithReuseIdentifier:@"cell"];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.arrInternet.count+1 + self.arrLocal.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PBCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.deleteIconTag = indexPath.item;
cell.imageViewTag = indexPath.item;
_imageView = cell.imageView;
cell.imageView.backgroundColor = [UIColor blackColor];
NSInteger totalCount = [self collectionView:collectionView numberOfItemsInSection:indexPath.section];
if (indexPath.item == (self.arrInternet.count+self.arrLocal.count)) {
cell.imageView.backgroundColor = [UIColor whiteColor];
}
//判断当前cell是否是网络图片,如果是网络图片则通过SDWebImage框架下载网络图片,否则是本地图片
if (indexPath.item+1 <= self.arrInternet.count) {
SDWebImageDownloader * downloader = [SDWebImageDownloader new];
[downloader downloadImageWithURL:[NSURL URLWithString:self.arrInternet[indexPath.item]] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (finished) {
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = image;
});
}
}];
}else{
//如果有本地图片并且不是最后一个cell则设置cell的图片为本地图片,因为最后一个cell是添加图片按钮
if (self.arrLocal.count && (indexPath.item != self.arrLocal.count + self.arrInternet.count)) {
cell.imageView.image = self.arrLocal[(indexPath.item- self.arrInternet.count)];
}
}
//判断是否是最后一个cell
if (totalCount - indexPath.item - 1 != 0) {
//如果不是最后一个cell则显示删除按钮
cell.viewDelete.hidden = NO;
}else{
//最后一个cell 隐藏删除按钮并将图片设置为nil
cell.viewDelete.hidden = YES;
cell.imageView.image = nil;
}
//设置点击删除按钮的点击事件回调.
[cell setDeleteCallBackBlock:^(UIButton *ges, NSInteger tag) {
[self clickDeleteIcon:ges andTag:tag];
}];
return cell;
}
#pragma mark - 删除图片
-(void)clickDeleteIcon:(UIButton *)gesture andTag:(NSInteger)tag{
//如果要删除图片的脚标 < 网络图片链接数组的count(个数),则用户删除的是网络图片,否则删除的是本地图片
NSLog(@"%zd-",tag);
if (tag+1 <= self.arrInternet.count) {
[self.arrInternet removeObjectAtIndex:tag];
//insert code what you want...
//在此处添加删除网络(服务器/数据库)图片的代码...
}else{
//删除本地图片
[self.arrLocal removeObjectAtIndex:(tag - self.arrInternet.count)];
}
//刷新collectionView
[self.collectionView reloadData];
}
#pragma mark - 点击cell添加本地图片
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//判断点击的是否是最后一个cell
if (indexPath.item == self.arrInternet.count + self.arrLocal.count) {
UIImagePickerController * pickerVc = [UIImagePickerController new];
pickerVc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:pickerVc animated:YES completion:^{
}];
pickerVc.delegate = self;
}
}
#pragma mark - 选中图片回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:^{
}];
//将选中的图片添加到本地图片数组
[self.arrLocal addObject:image];
[self.collectionView reloadData];
}
#pragma mark - 取消选择
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end