【问题标题】:UIImagepickercontrollerreferenceurl returns nilUIImagepickercontrollerreferenceurl 返回 nil
【发布时间】:2012-12-07 09:26:29
【问题描述】:

在从相机中选取图像后,此方法被调用。

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

谢谢

【问题讨论】:

    标签: iphone ios uiimagepickercontroller


    【解决方案1】:

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

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

    【讨论】:

    • 你是说它为什么会生病,但没有提到我们如何摆脱它?你能帮忙吗?
    【解决方案2】:

    我在获取所选照片的​​文件名时遇到了同样的问题。对于新拍摄的照片,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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      • 2019-06-20
      • 2017-12-16
      • 2014-09-27
      相关资源
      最近更新 更多