在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的代理

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)于選取相冊圖片的問題就先寫到這里。到目前為止只遇到過這些問題