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:@"圖片已保存到相冊"];
}
});
}
}
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。