2

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

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

谢谢

4

2 回答 2

3

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

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

于 2013-06-07T01:29:48.370 回答
0

我在获取所选照片的​​文件名时遇到了同样的问题。对于新拍摄的照片,UIImagePickerControllerReferenceURL 将返回 nil,因为 NSURL 不存在,所以我分配了一个默认名称。它 referenceUrl 存在我在照片框架中引入的 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)
    }
}
于 2015-12-16T11:47:16.750 回答