OC简单实现本地图片保存
首先导入头文件
#import <Photos/PHPhotoLibrary.h>
Ps: 不要忘记这个 NSPhotoLibraryAddUsageDescription
然后下载方法
- (void)buttonDownloadTarget{
//判断是不是可以下载
BOOL isSave = [self isCanDown];
__weak typeof(self) weakSelf = self;
if (isSave) {
// 保存图片到相册中 UIImageWriteToSavedPhotosAlbum(self.imageViewSave.image,self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
}else{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message: @"是否去开启相册权限" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertController animated:YES completion:nil];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[weakSelf openSet];
}];
UIAlertAction *actionS = [UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:action];
[alertController addAction:actionS];
}
}
//保存图片完成之后的回调 这个必须有
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
// Was there an error?
if (error != NULL)
{
NSLog(@"下载失败")
}
else
{
NSLog(@"下载成功")
}
}
//开启相册权限 直接跳过去
-(void)openSet{
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
//判断是不是可以下载
- (BOOL) isCanDown {
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted ||
status == PHAuthorizationStatusDenied) {
//无权限
return NO;
}
//有权限
return YES;
}
- (void)textFieldEditChanged:(UITextField *)textField
{
[self textFieldDidChange:textField interger:10];
}
干货就是这么简单,点个赞再走呀亲!!!