将图片保存到系统相册
// 保存图片到系统相册里
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
// image:保存的图片
// completionTarget:保存完成的监听者
// completionSelector:保存完成的时候调用监听者的方法
}
// 保存图片完成的时候调用,必须调用系统的这个方法,不可以自定义别的方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
[MBProgressHUD showError:@"保存失败"];
}else{
[MBProgressHUD showSuccess:@"保存成功"];
}
}
从系统相册选择图片
@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
...
@end
// 选择照片
- (IBAction)photo:(UIBarButtonItem *)sender {
// 创建照片选择控制器
UIImagePickerController *imagePickerVc = [[UIImagePickerController alloc] init];
// 设置数据源
imagePickerVc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; // 系统相册
// 设置代理
imagePickerVc.delegate = self;
//modal - 进入相册界面
[self presentViewController:imagePickerVc animated:YES completion:nil];
}
#pragma mark - <UIImagePickerControllerDelegate>
// 用户选择照片的时候调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
// 获取用户选择的照片
UIImage *image = info[UIImagePickerControllerOriginalImage];
// modal - 退出相册界面
[self dismissViewControllerAnimated:YES completion:nil];
_drawView.image = image;
}