2015-12-07 50 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)相機使用

相關問題