调用本地相册、相机
1、 判断系统是否支持相机
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]
2、 创建相册视图控制器对象
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
3、 设置sourceType(资源类型)
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;//相机
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//图片库
imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;//保存的图片
4、 设置弹出动画(可选)
imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
5、 设置是否允许编辑图片
imagePickerController.allowsEditing = YES;
6、 设置代理(UIImagePickerControllerDelegate)
imagePickerController.delegate = self;
7、 实现代理方法:获取图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
//获取图片
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
//取消点击
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
8、 保存图片到相册
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);// image:要保存的图片UImage对象,
- (void) image: (UIImage *) image
didFinishSavingWithError: (NSError *) error
contextInfo: (void *) contextInfo{
//如果error==NULL,则表示图片保存成功
}