如何获取从 UIImagePicker 拍摄的图片的资产 URL

How to get the asset URL of a picture taken from UIImagePicker

我正在尝试了解如何将图像的资产 URL 保存到相机胶卷,但我找不到太多可以帮助我的方法。我看过 Objective-C 个示例,但我没有这方面的经验,而且很多时候他们使用 ALAssetLibrary,现在已弃用。我所做的只是保存一张照片,但我需要获取 URL 以将其加载到另一个图像中,而该图像的 UIImageView 并不总是存在。尝试将其加载到视图会使应用程序崩溃。

代码:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: AnyObject]) {
    imagePicker.dismissViewControllerAnimated(true, completion: nil)
    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    UIImageWriteToSavedPhotosAlbum((info[UIImagePickerControllerOriginalImage] as? UIImage)!, nil, nil, nil)
    if let detail = self.detailItem {
        print(info.values)
        // ### Where I need to have the image URL ###
        //detail.thumbnail = info[UIImagePickerControllerReferenceURL] as! String
    }
}

来自 Apple 文档:The Assets Library framework is deprecated as of iOS 9.0. Instead, use the Photos framework instead, which in iOS 8.0 and later provides more features and better performance for working with a user’s photo library.

所以iOS8以后使用Photos API and placeholderForCreatedAsset of PHAssetChangeRequest

    PHPhotoLibrary.sharedPhotoLibrary().performChanges({
    let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(info[UIImagePickerControllerOriginalImage])
    let assetPlaceholder = assetRequest.placeholderForCreatedAsset
}, completionHandler: { success, error in
    // completion handling
})

// I have not tested but this should work

以下用于较旧的 iOS:

func writeImageToSavedPhotosAlbum(_ imageRef: CGImage!,
                         metadata metadata: [NSObject : AnyObject]!,
                  completionBlock completionBlock: ALAssetsLibraryWriteImageCompletionBlock!)