UIImagepickercontrollerreferenceurl返回nil

时间:2012-12-07 09:26:29

标签: iphone ios uiimagepickercontroller

从相机中挑选图像后,会调用此方法。

- (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。我不知道我做错了什么。请帮忙。

由于

2 个答案:

答案 0 :(得分:3)

没有参考URL,因为在您实际保存照片之前,文件系统上不存在照片。

MediaURL仅用于电影。它是保存在临时文件夹中的电影压缩版本的URL,可由您的应用程序访问。

答案 1 :(得分:0)

我在获取所选照片的​​文件名方面遇到了同样的问题。对于新拍摄的图片,UIImagePickerControllerReferenceURL将返回nil,因为NSURL不存在所以我指定了一个默认名称。它是referenceUrl存在我在PHImageManager的帮助下得到文件名,这是在Photos Framework中引入的。

    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)
    }
}