遇到一个问题:相机的界面总是显示英文。
查了下资料发现在 info.plist里面添加Localized resources can be mixed = YES ,表示是否允许应用程序获取框架库内语言(前题是手机要设置为中文)
1.首先遵循协议
<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>
2.根据需要选择调用相机/相册
这里我们一般会使用UIActionSheet中(需要遵循UIActionSheetDelegate协议)
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
UIActionSheetDelegate
根据被点击的按钮做出反应,0对应destructiveButton,之后的button依次排序
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(@"拍照");
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imageController = [[UIImagePickerController alloc] init];
[imageController setSourceType:UIImagePickerControllerSourceTypeCamera]; // 设置类型
[imageController setDelegate:self];
[imageController setAllowsEditing:YES]; // 设置是否可以编辑
[self presentViewController:imageController animated:YES completion:nil];
}else{
[self showAlertWith:@"提示" andMessage:@"哎呀,当前设备没有摄像头。"];
}
}else if (buttonIndex == 1) {
NSLog(@"相册");
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *imageController = [[UIImagePickerController alloc] init];
[imageController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];// 设置为相册类型
[imageController setDelegate:self];
[imageController setAllowsEditing:YES];
[self presentViewController:imageController animated:YES completion:nil];
}else{
[self showAlertWith:@"提示" andMessage:@"图片库不可用。"];
}
}
}
UIImagePickerControllerDelegate
当获取到照片或视频后调用
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"获取成功");
UIImage *pickedImage = nil;
if ([picker allowsEditing]) {
pickedImage = [info objectForKey:UIImagePickerControllerEditedImage];
} else {
pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
}
NSString *imagePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(pickedImage) writeToFile:imagePath atomically:YES];
self.imageView.image = [UIImage imageNamed:pickedImage]; // 使用
}