有時候我們需要把很多張圖片一次性下載到本地相冊或分享到微信朋友圈,此時就需要進行批量操作了

IMG_F0F6F1DA7588-1.jpeg

Jietu20180801-095931.gif
由于代碼量大,我就不在此一一細(xì)數(shù)了,需要的朋友可以在GitHub下載我的代碼閱讀
關(guān)鍵代碼
//把URL轉(zhuǎn)化成圖片存儲起來
-(void)urlTiImagesWithCompletionHandler:(void (^)(BOOL isSuccess))completionHandler{
_imageMuarr = [NSMutableArray new];
for (int i=0; i<[self imageUrls].count; i++) {
UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectZero];
[imageV sd_setImageWithURL:[NSURL URLWithString:[self imageUrls][i]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
[self.imageMuarr addObject:image];
if (self.imageMuarr.count==[self imageUrls].count) {
completionHandler(YES);
}
}];
}
}
//存儲已選的圖片
-(NSArray *)selectImages{
NSMutableArray *muArr = [NSMutableArray new];
for (int i=0; i<[self imageUrls].count; i++) {
for (int j=0; j<self.selectedImageMuarr.count; j++) {
if (i==[self.selectedImageMuarr[j] integerValue]) {
[muArr addObject:_imageMuarr[i]];
}
}
}
return muArr;
}
//保存圖片到本地相冊
-(void)saveImages{
if (_imageMuarr.count<[self imageUrls].count) {
[self urlTiImagesWithCompletionHandler:^(BOOL isSuccess) {
[self saveImages];
}];
return;
}
NSArray *selectImage = [self selectImages];
if (selectImage.count==0) {
[XLImageLoading showAlertInView:self.view message:@"請選擇圖片"];
return;
}
NSLog(@"***********%@", selectImage);
}