iOS-圖片多選效果

效果圖:

此功能我用了兩個(gè)第三方庫(kù):

https://github.com/iphone5solo/PYPhotoBrowser

https://github.com/banchichen/TZImagePickerController

PYPhotoBrowser使用Cocoapods安裝的方式:pod "PYPhotoBrowser"

然后導(dǎo)入主頭文件#import

TZImagePickerController使用Cocoapods安裝的方式:pod 'TZImagePickerController'

然后導(dǎo)入主頭文件#import "TZImagePickerController.h

如果不懂這兩個(gè)第三方 分別的作用可以打開(kāi)git地址 看看文檔

接受的代理為

TZImagePickerControllerDelegate,PYPhotosViewDelegate

定義一個(gè)@property (nonatomic, weak) PYPhotosView *publishPhotosView;屬性 保存選擇的圖片

點(diǎn)擊選擇添加圖片按鈕調(diào)用方法:

-(void)communicationClickEvent:(UIButton *)btn{?

? if (self.repeatClickInt !=2) {

? ? ? ? self.repeatClickInt = 2;? ?

? ? // 1. 常見(jiàn)一個(gè)發(fā)布圖片時(shí)的photosView?

? ? ? PYPhotosView *publishPhotosView = [PYPhotosView photosView];? ? ? ? publishPhotosView.py_x = 5;? ?

? ? publishPhotosView.py_y = 100;?

? ? ? // 2.1 設(shè)置本地圖片? ? ?

? publishPhotosView.images = nil;? ?

? ? // 3. 設(shè)置代理? ?

? ? publishPhotosView.delegate = self;? ? ?

? publishPhotosView.photosMaxCol = 5;//每行顯示最大圖片個(gè)數(shù)? ? ? ? publishPhotosView.imagesMaxCountWhenWillCompose = 9;//最多選擇圖片的個(gè)數(shù) ?

? ? ?// 4. 添加photosView ? ?

? ?[self.view addSubview:publishPhotosView];? ? ? ? self.publishPhotosView = publishPhotosView;?

? }?? ???

?}

需要完成的代理方法

#pragma mark - PYPhotosViewDelegate

- (void)photosView:(PYPhotosView *)photosView didAddImageClickedWithImages:(NSMutableArray*)images{// 在這里做當(dāng)點(diǎn)擊添加圖片按鈕時(shí),你想做的事。

[selfgetPhotos];

}

// 進(jìn)入預(yù)覽圖片時(shí)調(diào)用, 可以在此獲得預(yù)覽控制器,實(shí)現(xiàn)對(duì)導(dǎo)航欄的自定義

- (void)photosView:(PYPhotosView *)photosView didPreviewImagesWithPreviewControlelr:(PYPhotosPreviewController *)previewControlelr{

NSLog(@"進(jìn)入預(yù)覽圖片");

}

進(jìn)入相冊(cè)的方法:

-(void)getPhotos{

? ? CCWeakSelf;

? ? TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9-weakSelf.photos.count delegate:weakSelf];

? ? imagePickerVc.maxImagesCount=9;//最小照片必選張數(shù),默認(rèn)是0

? ? imagePickerVc.sortAscendingByModificationDate = NO;// 對(duì)照片排序,按修改時(shí)間升序,默認(rèn)是YES。如果設(shè)置為NO,最新的照片會(huì)顯示在最前面,內(nèi)部的拍照按鈕會(huì)排在第一個(gè)

? ? // 你可以通過(guò)block或者代理,來(lái)得到用戶選擇的照片.

[imagePickerVcsetDidFinishPickingPhotosHandle:^(NSArray?*photos,NSArray*assets,BOOLisSelectOriginalPhoto){

? ? ? ? NSLog(@"選中圖片photos === %@",photos);

//? ? ? ? for (UIImage *image in photos) {

//? ? ? ? ? ? [weakSelf requestData:image];//requestData:圖片上傳方法 在這里就不貼出來(lái)了

//? ? ? ? }

? ? ? ? [weakSelf.photosaddObjectsFromArray:photos];

? ? ? ? [self.publishPhotosView reloadDataWithImages:weakSelf.photos];

? ? }];

? ? [weakSelfpresentViewController:imagePickerVc animated:YES completion:nil];

}

就這么完成了,附上原作者鏈接:http://www.itdecent.cn/p/6181f32bb844

附上Demo 鏈接: ?https://pan.baidu.com/s/187Ufxt1aVJDAA81JW4sK1w 密碼:a5qw

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容