2015-12-07 141 views
2

我使用下面的代码从相机或照片库中选择图像。在iOS 8中,它很好地挑选图像。但在iOS 9中。显示选择器视图但不选择图像。甚至没有回到控制器。点击图片什么都不做。我究竟做错了什么。UIImagePickerController不在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]; 
    } 
} 
+0

你是否实现了图像选取器的委托方法? – cekisakurek

+0

是的,我说它在iOS 8中正常工作。 – user2096064

+0

然后向我们展示你的委托方法的实现。他们叫吗? – jcaron

回答

0

按照下面的编码

- (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]; 
} 
2

UIPopoverController在iOS版9已经过时相反,你应该使用UIViewControllermodalPresentationStyle设置为UIModalPresentationPopover

例如:

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; 

注:UIModalPresentationPopover在iOS版8.0中引入的,如果您需要前8.0支持,那么你将需要一些条件检查使用旧代码的iOS 7,以上在iOS 8+。

注2:我相信上面的技术也足够聪明的工作,如果它应该有模式在iPhone目前的选择器代替酥料饼(通过大小类),所以我不认为你需要做的IS_IPAD检查。

1

使用此代码它在ios 9中工作正常。我使用操作表来显示2个选项来选择。

-(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; 


} 

为iPad,请参照以下链接

find here

2

我发现,我的应用程序正在运行的其他iPhone手机好(无论是iOS的8或iOS 9)。我无法从任何其他应用中选择图片。然后我决定重置我的iPhone。现在一切都很完美。这不是代码问题。这是我的iPhone问题。

+0

刚刚发布的问题完全相同的问题...然后重新启动我的手机......它的工作! o.O –

0

由于动作片已被取消,UIAlertController需要使用,我写了类似的回答新手的好处:

- (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]; 
} 
0

,请点击此按如下步骤操作,从库中选取的图像,并可以采取使用“UIAlertController”你只要把下面的代码在你的按钮点击事件

- (IBAction)yourButtonClickEvent:(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]; 

}

然后从后摄像机图像你只要把UIImagePicker委托方法是这样的:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info 

{ 
UIImage *imagePicked = info[UIImagePickerControllerOriginalImage]; 
self.imgForUpaloadDocument.image = imagePicked; 
[picker dismissViewControllerAnimated:YES completion:nil]; 

if(picker.sourceType == UIImagePickerControllerSourceTypeCamera) 
{ 
    UIImageWriteToSavedPhotosAlbum(imagePicked, nil, nil, nil); 
} 
} 


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
    { 
    [picker dismissViewControllerAnimated:YES completion:nil]; 
    } 
0

与上面的代码,我想补充一点,你可能需要实现UIImage的选择器视图 - 控制。

如果你想防止应用程序崩溃,你需要提供一个描述你为什么要访问摄像机,照片库等。这是iOS10的新功能。

将以下内容输入到Info.plist文件中。

照片

重点:隐私 - 图片库使用情况说明价值:$(PRODUCT_NAME)的照片使用

相机

重点:隐私 - 相机用法说明价值:$(PRODUCT_NAME)相机使用

相关问题