在Xcode8环境下将项目运行在iOS10的设备/模拟器中,访问相册和相机需要额外配置info.plist文件。分别是Privacy - Photo Library Usage Description和Privacy - Camera Usage Description字段,详见Demo中info.plist中的设置。 最好在相对应的后面写上中文这个是我在最近审核的时候被拒的原因之一
首先要遵循以下PickerController的代理
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.editing = YES;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
//创建sheet提示框,提示选择相机还是相册
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"请选择打开方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//相机选项
UIAlertAction * camera = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//选择相机时,设置UIImagePickerController对象相关属性
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
//跳转到UIImagePickerController控制器弹出相机
[self presentViewController:imagePicker animated:YES completion:nil];
}];
//相册选项
UIAlertAction * photo = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//选择相册时,设置UIImagePickerController对象相关属性
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//跳转到UIImagePickerController控制器弹出相册
[self presentViewController:imagePicker animated:YES completion:nil];
}];
//取消按钮
UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
//添加各个按钮事件
[alert addAction:camera];
[alert addAction:photo];
[alert addAction:cancel];
//弹出sheet提示框
[self presentViewController:alert animated:YES completion:nil];
这个是调取相册和相机的代码
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//赋值并且移除控制器
self.imageview.image = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
接下来写一个我今天遇到的问题。 是在一个控制器里面有两个获取相册图片的按钮
想让两个UIImageView显示不同的图片。目前有两种方法(我自己现在知道的)
1.就是在按钮点击的时候分别设置按钮的选中状态 然后根据选中状态来判断
-(void)erweimabtnclick:(UIButton * )sender{
sender.selected = YES;
self.selectimagebtn = sender;
[self userphotolibrary];
}
当然在为图片赋完值后要吧按钮的选中状态设置为NO;
2.声明一个全局属性的 int 类型的属性 分别在按钮点击的时候给它赋值 再根据int的值去判断
if (i==1){
self.erweimaimage.image = info[UIImagePickerControllerOriginalImage];
self.selecterweimabtn.selected = NO;
}
关于选取相册图片的问题就先写到这里。到目前为止只遇到过这些问题