我采用的是系统的方法,没有自定义界面
//拍照
- (void)takePhoto
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
} else {
[[UIAlertView alertViewWithTitle:@"提示" message:@"当前设备相机不可用" buttonTitles:@[@"确定"] callback:^(UIAlertView *alertView, NSInteger buttonIndex) {
}] show];
}
}
//从相册选择
- (void)loadLocal
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
代理方法:
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
/**
选取的信息都在info里面
键值:
NSString *const UIImagePickerControllerMediaType ;指定用户选择的媒体类型
NSString *const UIImagePickerControllerOriginalImage ;原始图片
NSString *const UIImagePickerControllerEditedImage ;裁剪之后的图片
NSString *const UIImagePickerControllerCropRect ;裁剪尺寸
NSString *const UIImagePickerControllerMediaURL ;媒体的URL
NSString *const UIImagePickerControllerReferenceURL ;原件的URL
NSString *const UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效
*/
UIImage *editedImage = info[UIImagePickerControllerEditedImage];
[self updatePortraitWithImage:editedImage];
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
enum {
UIImagePickerControllerSourceTypePhotoLibrary ,
//来自图库
UIImagePickerControllerSourceTypeCamera ,
//来自相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum
//来自相册
};
if
([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
NSLog(@
"支持相机"
);
}
if
([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
NSLog(@
"支持图库"
);
}
if
([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
NSLog(@
"支持相片库"
);
}