self.videoSuffixs = @[@"mov",@"mp4",@"rmvb",@"rm",@"flv",@"avi",@"3gp",@"wmv",@"mpeg1",@"mpeg2",@"mpeg4(mp4)", @"asf",@"swf",@"vob",@"dat",@"m4v",@"f4v",@"mkv",@"mts",@"ts"];
- (void)addLongPress {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) {
@strongify(self);
NSString *urlString = (NSString *)photo;
NSString *suffix = [[urlString.lowercaseString componentsSeparatedByString:@"."] lastObject];
if (suffix && [self.videoSuffixs containsObject:suffix]) {
[MBProgressHUD showInfoMessage:@"暂不支持视频保存"];
return;
}
NSArray *actions;
if (self.photos.count > 1) {
actions = @[@"取消", @"保存该图片至相册", @"批量保存至相册"];
} else {
actions = @[@"取消", @"保存该图片至相册"];
}
[self ActionSheetWithTitle:nil message:nil destructive:nil destructiveAction:^(NSInteger index) {
DLog(@"destructiveAction = %ld", index);
} andOthers:actions animated:YES action:^(NSInteger index) {
DLog(@"action = %ld", index);
DLog(@"photo = %@", photo);
DLog(@"photos = %@", self.photos);
if (index == 0) {
// 取消
} else if (index == 1) {
// 保存该图片至相册
self->_isSaveMultiPhotos = NO;
SDImageCache *imageCache = [SDImageCache sharedImageCache];
UIImage *image = [imageCache imageFromCacheForKey:photo];
[self saveImgAsync:image];
} else if (index == 2) {
// 批量保存至相册
self->_isSaveMultiPhotos = YES;
[self saveImgsAsync:self.photos];
}
}];
}];
[cell addGestureRecognizer:longPress];
}
- (void)saveImgsAsync:(NSArray *)photos {
[MBProgressHUD showInfoMessage:@"正在保存"];
if(photos.count>0) {
dispatch_group_t group = dispatch_group_create();
for (NSString *photo in photos) {
dispatch_group_enter(group);
NSURL *url = [NSURL URLWithString:photo];
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
[self saveImgAsync:image];
dispatch_group_leave(group);
}];
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
[MBProgressHUD showInfoMessage:@"图片已保存到相册"];
});
}
}
- (void)saveImgAsync:(UIImage *)img
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
});
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (_isSaveMultiPhotos == NO) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
[MBProgressHUD showErrorMessage:@"图片保存失败"];
} else {
[MBProgressHUD showSuccessMessage:@"图片已保存到相册"];
}
});
}
}