2016-06-20 4 views
4

私のアプリは、カメラを使用して、iPadのための問題で、iTunesから拒否されてきています。何らかの理由で、それはdidFinishPickingMediaWithInfo方法でクラッシュします。これは、iPhoneで正常に動作しますが、私はそれがiPadで動作するdoesntの理由はわかりません。ナシエラー(iPhone上で動作します)

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 

    if(picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary) 
    { 
     //Crashing here -> "fatal error: unexpectedly found nil while unwrapping an Optional value" 
     var selectedImage: UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage 

     appraisalPic.backgroundColor = UIColor.blackColor(); 
     appraisalPic.image = selectedImage 

     self.dismissViewControllerAnimated(true, completion: nil) 


    } 

enter image description here

+0

イメージピッカーを表示する前に 'if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary)'をチェックする必要があります。 –

+0

はまた、構文をチェックし、それがあるべき 'FUNCのimagePickerController(ピッカー:UIImagePickerController、didFinishPickingMediaWithInfo情報:[文字列:ANYOBJECT]){... }' –

答えて

2

上記のコードの代わりに、下記の使用、それはあなたを助けるかもしれないのです。あなたが行を次のようにオプションの値のアンラップを余儀なくされているので

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 

      if(picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary) 
      { 

       let url: NSString = info[UIImagePickerControllerReferenceURL] as NSString 
       var imageName:String = url.lastPathComponent  
       let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory 
       let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask 

       if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) { 
        if paths.count > 0 { 
        if let dirPath = paths[0] as? String { 
        let readPath = dirPath.stringByAppendingPathComponent(imageName) 
        var pickedimage = UIImage(CGImage: UIImage(contentsOfFile: readPath)!.CGImage, scale: 1.0, orientation: .Up) 
        appraisalPic.backgroundColor = UIColor.blackColor(); 
        appraisalPic.image = pickedimage 
       } 
      } 
     } 
} 
1

クラッシュが発生します。

var selectedImage: UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage 

は、次のようにコードを書き換えてみます。ところで

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

    guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage where picker.sourceType == .PhotoLibrary else { 
     return 
    } 

    appraisalPic.backgroundColor = UIColor.blackColor() 
    appraisalPic.image = selectedImage 

    self.dismissViewControllerAnimated(true, completion: nil) 
} 

:あなたはpicker.sourceTypeを設定します正しく?

let picker = UIImagePickerController() 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // ... 
    picker.sourceType = .PhotoLibrary 
    // ... 
} 

はまたGitHub's Swift Style Guide、特にAvoid Using Force-Unwrapping of Optionalsをお読みください。あなたがここにpresentViewController

を使用してイメージピッカーを提示する必要がiPad上

1

は、iPhoneとiPadの両方に提示するための私のObj-Cのコードです。

-(void)userPhotoLibrary{ 
    UIImagePickerController *p; 
    p = [[UIImagePickerController alloc]init]; 

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { 
     [p setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
     [p setDelegate:self]; 
     p.allowsEditing = YES; 

     [self presentViewController:p animated:YES completion:nil]; 
    }else{ 
     NSString *alertTitle = NSLocalizedString(@"NoPhotosAlertTitle", @"The photos are not available title"); 
     NSString *alertMessage = NSLocalizedString(@"NoPhotosAlertMessage", @"The photos are not available message"); 
     NSString *alertContinue = NSLocalizedString(@"NoPhotosAlertContinueButton", @"The photos are not available continue button label"); 
     UIAlertController *alert = [UIAlertController alertControllerWithTitle:alertTitle message:alertMessage preferredStyle:UIAlertControllerStyleAlert]; 
     UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
                   handler:^(UIAlertAction * action) {}]; 
     UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:alertContinue style:UIAlertActionStyleDefault 
                  handler:^(UIAlertAction * action) {}]; 
     [alert addAction:defaultAction]; 
     [alert addAction:cancelAction]; 
     [self presentViewController:alert animated:YES completion:nil]; 

    } 

} 
関連する問題