iOS相機(jī)調(diào)用基本使用方法

iOS更新后,相機(jī)模塊有路一次大的版本調(diào)整,最重要的就是添加了權(quán)限設(shè)置,關(guān)于權(quán)限設(shè)置這里,我們可以把plist文件用sourceCode方式打開,添加

NSCameraUsageDescription? ? cameraDesciptio

NSContactsUsageDescription? ? contactsDesciption

NSMicrophoneUsageDescription? ? microphoneDesciption

NSPhotoLibraryUsageDescription? ? 此 App 需要您的同意才能讀取媒體資料庫

寫完之后就是這樣的

對(duì)的,這個(gè)時(shí)候我們環(huán)境就做好了,我們就可以進(jìn)行下一步了,遵循協(xié)議方法

這是兩個(gè)協(xié)議方法

以及全局變量

@property (nonatomic,strong) UIImagePickerController *pickerController;

接著是具體的實(shí)現(xiàn)方法了

- (void)touchUpSenderForBtn {

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"照片選擇方式" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"相機(jī)" 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:@"相冊(cè)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])

{

self.pickerController.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;

self.pickerController.allowsEditing = YES;

//打開相冊(cè)

[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

{

//如果實(shí)現(xiàn)了這個(gè)代理方法 必須手動(dòng)退出相冊(cè)界面

[self dismissViewControllerAnimated:YES completion:nil];

UIImage *selectImage = info[@"UIImagePickerControllerEditedImage"];

if (_isLeft) {//這里的selectImage就是我們選擇的圖片了 我們只需要賦值就好了

_leftBtn.image = selectImage;

}else {

_rightBtn.image = selectImage;

}

}

這里最基本的步驟就已經(jīng)寫完了,新人第一次發(fā)帖子,求評(píng)論,在現(xiàn)在這個(gè)iOS開發(fā)多如狗的年代,我只能不斷進(jìn)步,多寫一點(diǎn)東西,來鼓勵(lì)自己。同樣你們也可以鼓勵(lì)我的說,這些也是我在網(wǎng)上找了好久才找到的,不容易。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容