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開發多如狗的年代,我只能不斷進步,多寫一點東西,來鼓勵自己。同樣你們也可以鼓勵我的說,這些也是我在網上找了好久才找到的,不容易。