iOS關(guān)于調(diào)取相冊選取圖片的問題

在Xcode8環(huán)境下將項目運(yùn)行在iOS10的設(shè)備/模擬器中,訪問相冊和相機(jī)需要額外配置info.plist文件。分別是Privacy - Photo Library Usage Description和Privacy - Camera Usage Description字段,詳見Demo中info.plist中的設(shè)置。 最好在相對應(yīng)的后面寫上中文這個是我在最近審核的時候被拒的原因之一

首先要遵循以下PickerController的代理


//創(chuàng)建UIImagePickerController對象,并設(shè)置代理和可編輯

UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];

imagePicker.editing = YES;

imagePicker.delegate = self;

imagePicker.allowsEditing = YES;

//創(chuàng)建sheet提示框,提示選擇相機(jī)還是相冊

UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"請選擇打開方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

//相機(jī)選項

UIAlertAction * camera = [UIAlertAction actionWithTitle:@"相機(jī)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

//選擇相機(jī)時,設(shè)置UIImagePickerController對象相關(guān)屬性

imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;

imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;

//跳轉(zhuǎn)到UIImagePickerController控制器彈出相機(jī)

[self presentViewController:imagePicker animated:YES completion:nil];

}];

//相冊選項

UIAlertAction * photo = [UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

//選擇相冊時,設(shè)置UIImagePickerController對象相關(guān)屬性

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

//跳轉(zhuǎn)到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];

這個是調(diào)取相冊和相機(jī)的代碼

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

//賦值并且移除控制器

self.imageview.image = info[UIImagePickerControllerOriginalImage];

[picker dismissViewControllerAnimated:YES completion:nil];

}

接下來寫一個我今天遇到的問題。 是在一個控制器里面有兩個獲取相冊圖片的按鈕?

想讓兩個UIImageView顯示不同的圖片。目前有兩種方法(我自己現(xiàn)在知道的)

1.就是在按鈕點(diǎn)擊的時候分別設(shè)置按鈕的選中狀態(tài) 然后根據(jù)選中狀態(tài)來判斷

-(void)erweimabtnclick:(UIButton * )sender{

sender.selected = YES;

self.selectimagebtn = sender;

[self userphotolibrary];

}

當(dāng)然在為圖片賦完值后要吧按鈕的選中狀態(tài)設(shè)置為NO;

2.聲明一個全局屬性的 int 類型的屬性 分別在按鈕點(diǎn)擊的時候給它賦值 再根據(jù)int的值去判斷

if (i==1){

self.erweimaimage.image = info[UIImagePickerControllerOriginalImage];

self.selecterweimabtn.selected = NO;

}

關(guān)于選取相冊圖片的問題就先寫到這里。到目前為止只遇到過這些問題

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容