2012-12-07 51 views
2

從相機中選取圖像後,此方法被調用。UIImagepickercontrollerreferenceurl返回無

- (void) imagePickerController: (UIImagePickerController *) picker 
didFinishPickingMediaWithInfo: (NSDictionary *) info { 

[picker dismissModalViewControllerAnimated: YES]; 

// cameraImage.image = [info objectForKey: 
//       UIImagePickerControllerOriginalImage]; 

NSLog(@"%@",info); 

NSURL *url = [info valueForKey:UIImagePickerControllerReferenceURL]; 

NSLog(@"%@",url); 
NSLog(@"%@",[info valueForKey:UIImagePickerControllerMediaURL]); 
} 

我從UIImagePickerControllerOriginalImage獲取圖像。但是UIImagePickerControllerReferenceURL返回nil。我不知道我做錯了什麼。請幫忙。

感謝

回答

3

沒有參考URL,因爲照片沒有在文件系統中,直到你真正保存。

MediaURL僅用於電影。它是保存在臨時文件夾中的電影壓縮版本的URL,可由應用程序訪問。

+0

你是在說爲什麼它生病了,但沒有提及我們如何擺脫它?你能幫忙嗎? – Umitk

0

我有同樣的問題獲取所選照片的​​文件名。對於新拍攝的圖片,UIImagePickerControllerReferenceURL將返回nil,因爲NSURL不存在,所以我分配一個默認名稱。它referenceUrl存在,我得到了在Photos Framework中引入的PHImageManager的幫助文件名。

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
    self.selectedIndex = 1 
    var fileName = UIImage.defaultFileName() 

    //Pic is comming from library so url exists 
    if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? NSURL, image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     if #available(iOS 8.0, *) { 
      let phAsset = PHAsset.fetchAssetsWithALAssetURLs([referenceUrl], options: nil).lastObject as! PHAsset 
      PHImageManager.defaultManager().requestImageDataForAsset(phAsset, options: PHImageRequestOptions(), resultHandler: { (imagedata, dataUTI, orientation, info) in 
       if info!.keys.contains(NSString(string: "PHImageFileURLKey")) { 
        let path = info![NSString(string: "PHImageFileURLKey")] as! NSURL 
        fileName = path.lastPathComponent! 
       } 
       self.addDocumentDelegate?.tabBarControllerDidTakePicture(self, image: image, fileName: fileName) 
       AppRoot.sharedInstance.dismissModalViewController(true, completion: nil) 
      }) 
     } else { 
      self.addDocumentDelegate?.tabBarControllerDidTakePicture(self, image: image, fileName: fileName) 
      AppRoot.sharedInstance.dismissModalViewController(true, completion: nil) 
     } 
    } else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { //Pic is comming from Camera so url does not exists 
     self.addDocumentDelegate?.tabBarControllerDidTakePicture(self, image: image, fileName: fileName) 
     AppRoot.sharedInstance.dismissModalViewController(true, completion: nil) 
    } 
}