TZImagePreviewController
TZImagePreviewController copied to clipboard
能保存图片到相册吗?或者自定义按钮
老哥,来个PR哈
同需
configBottomToolBar方法里添加
_doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
_doneButton.titleLabel.font = [UIFont systemFontOfSize:13];
[_doneButton addTarget:self action:@selector(doneButtonClick) forControlEvents:UIControlEventTouchUpInside];
[_doneButton setTitle:@"保存" forState:UIControlStateNormal];
[_doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_doneButton setBackgroundColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.4]];
_doneButton.layer.cornerRadius = 3.0f;
_doneButton.layer.masksToBounds = YES;
再实现保存方法
id photo = _photos[self.currentIndex];
if ([photo isKindOfClass:[UIImage class]]) {
UIImageWriteToSavedPhotosAlbum(photo, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
} else if ([photo isKindOfClass:[NSURL class]]) {
NSURL *URL = (NSURL *)photo;
NSString *suffix = [[URL.absoluteString.lowercaseString componentsSeparatedByString:@"."] lastObject];
if (suffix && [self.videoSuffixs containsObject:suffix]) {
BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([URL path]);
if (compatible) {
UISaveVideoAtPathToSavedPhotosAlbum([URL path], self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
} else {
[self showHintMessage:@"不支持的类型"];
}
} else if ([suffix isEqualToString:@"gif"]) {
NSData *imageData;
if ([[SDImageCache sharedImageCache]diskImageDataExistsWithKey:URL.absoluteString]) {
imageData = [[SDImageCache sharedImageCache]diskImageDataForKey:URL.absoluteString];
} else {
imageData = [NSData dataWithContentsOfURL:URL];
}
ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
[library writeImageDataToSavedPhotosAlbum:imageData metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
[self showHintMessage:@"保存成功"];
}];
} else {
UIImage *img;
if ([[SDImageCache sharedImageCache]diskImageDataExistsWithKey:URL.absoluteString]) {
img = [[SDImageCache sharedImageCache]imageFromDiskCacheForKey:URL.absoluteString];
} else {
NSData *data = [NSData dataWithContentsOfURL:URL];
img = [UIImage imageWithData:data];
}
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
} else {
[self showHintMessage:@"不支持的类型"];
}