iOS更新后谓着,相機模塊有路一次大的版本調(diào)整泼诱,最重要的就是添加了權(quán)限設(shè)置,關(guān)于權(quán)限設(shè)置這里赊锚,我們可以把plist文件用sourceCode方式打開治筒,添加
NSCameraUsageDescription? ? cameraDesciptio
NSContactsUsageDescription? ? contactsDesciption
NSMicrophoneUsageDescription? ? microphoneDesciption
NSPhotoLibraryUsageDescription? ? 此 App 需要您的同意才能讀取媒體資料庫
對的,這個時候我們環(huán)境就做好了舷蒲,我們就可以進行下一步了耸袜,遵循協(xié)議方法
以及全局變量
@property (nonatomic,strong) UIImagePickerController *pickerController;
接著是具體的實現(xiàn)方法了
- (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
{
//如果實現(xiàn)了這個代理方法 必須手動退出相冊界面
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *selectImage = info[@"UIImagePickerControllerEditedImage"];
if (_isLeft) {//這里的selectImage就是我們選擇的圖片了 我們只需要賦值就好了
_leftBtn.image = selectImage;
}else {
_rightBtn.image = selectImage;
}
}
這里最基本的步驟就已經(jīng)寫完了,新人第一次發(fā)帖子牲平,求評論堤框,在現(xiàn)在這個iOS開發(fā)多如狗的年代,我只能不斷進步纵柿,多寫一點東西蜈抓,來鼓勵自己。同樣你們也可以鼓勵我的說昂儒,這些也是我在網(wǎng)上找了好久才找到的沟使,不容易。