문제 설명
iOS 9에서 UIImagePickerController가 이미지를 선택하지 않음 (UIImagePickerController not picking image in iOS 9)
아래 코드를 사용하여 카메라 또는 사진 라이브러리에서 이미지를 선택하고 있습니다. iOS 8에서는 이미지를 멋지게 선택하고 있습니다. 그러나 iOS 9에서는 . 선택기 보기가 표시되지만 이미지를 선택하지 않습니다. 컨트롤러로 돌아가지도 않습니다. 그림을 탭하면 아무 작업도 수행되지 않습니다. 내가 뭘 잘못하고 있죠.
‑ (void)showImgaePickerViewForSourceType:(UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = sourceType;
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
self.imgPickerController = imagePickerController;
if (IS_IPAD) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:self.imgPickerController];
[popover presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2‑200, self.view.frame.size.height/2 ‑ 300, 400, 400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
self.popOver = popover;
}];
}else{
[self presentViewController:self.imgPickerController animated:YES completion:NULL];
}
}
참조 솔루션
방법 1:
UIPopoverController
is deprecated in iOS 9. Instead you should be using UIViewController
with a modalPresentationStyle
set to UIModalPresentationPopover
.
For example:
UIImagePickerController *imagePickerController = ...;
CGRect sourceRect = CGRectMake(self.view.frame.size.width/2‑200, self.view.frame.size.height/2 ‑ 300, 400, 400);
imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:imagePickerController animated:YES completion:nil];
imagePickerController.popoverPresentationController.sourceRect = sourceRect;
imagePickerController.popoverPresentationController.sourceView = self.view;
Note: UIModalPresentationPopover
was introduced in iOS 8.0, if you need to support prior to 8.0 then you will need some conditional checks to use your old code in iOS 7 and the above in iOS 8+.
Note 2: I believe the technique above is also smart enough to work out if it should modally present the picker instead of a popover on iPhones (via size classes) so I don't think you need to do your IS_IPAD
check.
방법 2:
I found that my app was running good on other iPhones(whether iOS 8 or iOS 9). I was not able to pick image from any other app. Then i decided to reset my iPhone. Now everything is perfect. It was'nt the code issue. It was my iPhone issue.
방법 3:
Use this code it is working fine in ios 9.I am using action sheet to show 2 options to choose.
‑(IBAction)imagepickertapped:(id)sender
{
UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Take Photo" otherButtonTitles:@"Photo Library",nil];
popup.tag =909;
popup.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[popup showInView:self.view];
}
‑(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (actionSheet.tag ==909)
{
if (buttonIndex == 0)
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
// picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,kUTTypeImage,nil];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
}
else
{
UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:@"Camera Not Available" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[altnot show];
}
} else if (buttonIndex == 1) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,kUTTypeImage,nil];
picker.delegate = self;
picker.editing = YES;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
}
}
‑ (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo{
[picker dismissViewControllerAnimated:YES completion:NULL];
imagedata = UIImageJPEGRepresentation(image, 0.3);
Userimage.image = image;
}
For IPAD please refers this link
방법 4:
Follow the below coding
‑ (void)imagePicker
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:pickerTakePhoto animated:YES completion:nil];
}
‑ (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *imagePicked = info[UIImagePickerControllerEditedImage];
picker.delegate = self;
[picker dismissViewControllerAnimated:YES completion:nil];
}
‑ (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
방법 5:
Since action sheets are deprecated and UIAlertController needs to be used, I am writing a similar answer for the benefit of newbies:
‑ (IBAction)attachImageTapped:(id)sender
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* pickFromGallery = [UIAlertAction actionWithTitle:@"Take a photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
}
}];
UIAlertAction* takeAPicture = [UIAlertAction actionWithTitle:@"Choose from gallery"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
}];
[alertController addAction:pickFromGallery];
[alertController addAction:takeAPicture];
[alertController addAction:cancel];
[self presentViewController:alertController animated:YES completion:nil];
}
‑(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage *imagePicked = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
‑(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
(by user2096064、liamnichols、user2096064、Anil solanki、user3182143、Koushik Ravikumar)