效果图:
此功能我用了两个第三方库:
https://github.com/iphone5solo/PYPhotoBrowser
https://github.com/banchichen/TZImagePickerController
PYPhotoBrowser使用Cocoapods安装的方式:pod "PYPhotoBrowser"
然后导入主头文件#import
TZImagePickerController使用Cocoapods安装的方式:pod 'TZImagePickerController'
然后导入主头文件#import "TZImagePickerController.h
如果不懂这两个第三方 分别的作用可以打开git地址 看看文档
接受的代理为
TZImagePickerControllerDelegate,PYPhotosViewDelegate
定义一个@property (nonatomic, weak) PYPhotosView *publishPhotosView;属性 保存选择的图片
点击选择添加图片按钮调用方法:
-(void)communicationClickEvent:(UIButton *)btn{
if (self.repeatClickInt !=2) {
self.repeatClickInt = 2;
// 1. 常见一个发布图片时的photosView
PYPhotosView *publishPhotosView = [PYPhotosView photosView]; publishPhotosView.py_x = 5;
publishPhotosView.py_y = 100;
// 2.1 设置本地图片
publishPhotosView.images = nil;
// 3. 设置代理
publishPhotosView.delegate = self;
publishPhotosView.photosMaxCol = 5;//每行显示最大图片个数 publishPhotosView.imagesMaxCountWhenWillCompose = 9;//最多选择图片的个数
// 4. 添加photosView
[self.view addSubview:publishPhotosView]; self.publishPhotosView = publishPhotosView;
}
}
需要完成的代理方法
#pragma mark - PYPhotosViewDelegate
- (void)photosView:(PYPhotosView *)photosView didAddImageClickedWithImages:(NSMutableArray*)images{// 在这里做当点击添加图片按钮时,你想做的事。
[selfgetPhotos];
}
// 进入预览图片时调用, 可以在此获得预览控制器,实现对导航栏的自定义
- (void)photosView:(PYPhotosView *)photosView didPreviewImagesWithPreviewControlelr:(PYPhotosPreviewController *)previewControlelr{
NSLog(@"进入预览图片");
}
进入相册的方法:
-(void)getPhotos{
CCWeakSelf;
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9-weakSelf.photos.count delegate:weakSelf];
imagePickerVc.maxImagesCount=9;//最小照片必选张数,默认是0
imagePickerVc.sortAscendingByModificationDate = NO;// 对照片排序,按修改时间升序,默认是YES。如果设置为NO,最新的照片会显示在最前面,内部的拍照按钮会排在第一个
// 你可以通过block或者代理,来得到用户选择的照片.
[imagePickerVcsetDidFinishPickingPhotosHandle:^(NSArray *photos,NSArray*assets,BOOLisSelectOriginalPhoto){
NSLog(@"选中图片photos === %@",photos);
// for (UIImage *image in photos) {
// [weakSelf requestData:image];//requestData:图片上传方法 在这里就不贴出来了
// }
[weakSelf.photosaddObjectsFromArray:photos];
[self.publishPhotosView reloadDataWithImages:weakSelf.photos];
}];
[weakSelfpresentViewController:imagePickerVc animated:YES completion:nil];
}
就这么完成了,附上原作者链接:https://www.jianshu.com/p/6181f32bb844
附上Demo 链接: https://pan.baidu.com/s/187Ufxt1aVJDAA81JW4sK1w 密码:a5qw
网友评论