iOS更新后,相机模块有路一次大的版本调整,最重要的就是添加了权限设置,关于权限设置这里,我们可以把plist文件用sourceCode方式打开,添加
NSCameraUsageDescription cameraDesciptio
NSContactsUsageDescription contactsDesciption
NSMicrophoneUsageDescription microphoneDesciption
NSPhotoLibraryUsageDescription 此 App 需要您的同意才能读取媒体资料库
对的,这个时候我们环境就做好了,我们就可以进行下一步了,遵循协议方法
以及全局变量
@property (nonatomic,strong) UIImagePickerController *pickerController;
接着是具体的实现方法了
- (void)touchUpSenderForBtn {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"照片选择方式" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
self.pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.pickerController animated:YES completion:nil];
}
else
{
NSLog(@"打开失败");
}
}];
[alertController addAction:actionOne];
UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
self.pickerController.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
self.pickerController.allowsEditing = YES;
//打开相册
[self presentViewController:self.pickerController animated:YES completion:nil];
}
}];
[alertController addAction:actionTwo];
UIAlertAction *actionThree = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:actionThree];
[self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark - ImagePickerController delegate
- (UIImagePickerController *)pickerController
{
if (!_pickerController)
{
_pickerController=[[UIImagePickerController alloc]init];
_pickerController.delegate=self;
}
return _pickerController;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
//如果实现了这个代理方法 必须手动退出相册界面
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *selectImage = info[@"UIImagePickerControllerEditedImage"];
if (_isLeft) {//这里的selectImage就是我们选择的图片了 我们只需要赋值就好了
_leftBtn.image = selectImage;
}else {
_rightBtn.image = selectImage;
}
}
这里最基本的步骤就已经写完了,新人第一次发帖子,求评论,在现在这个iOS开发多如狗的年代,我只能不断进步,多写一点东西,来鼓励自己。同样你们也可以鼓励我的说,这些也是我在网上找了好久才找到的,不容易。