要遵循两个代理 UINavigationControllerDelegate,UIImagePickerControllerDelegate
其实就是用到 特定的控制器 UIImagePickerController
//1.将图片写入相册(就一句话,很简单)
UIImageWriteToSavedPhotosAlbum([UIImage imageNamed:@"bj1.jpg"], nil, nil, nil);
//2.打开相册,选择图片,显示到桌面上
//相册控制器
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//设置代理
picker.delegate = self;
//设置类型
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//用模态视图跳到相册控制器
[self presentViewController:picker animated:YES completion:nil];
//用到代理方法
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
//这个image 就是你选中后的图片(由于是系统自带的相册所以一次只能选中一张)
self.imageView.image = image;
//返回主页面
[picker dismissViewControllerAnimated:YEScompletion:nil];
//将图片转换为 NSData格式,下面就可以上传到服务器等
NSData *data =UIImagePNGRepresentation(image);
}