调用系统相册

遇到一个问题:相机的界面总是显示英文。 查了下资料发现在 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];    // 使用
}
复制代码

转载于:https://juejin.im/post/5a30e52b6fb9a04528467b08

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值