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

时间:2015-12-27 05:54:34

标签: ios swift image uiimagepickercontroller photokit

我正在尝试找出如何将图像的资产网址保存到相机胶卷,但我找不到太多可以帮助我的东西。我已经看过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
    }
}

1 个答案:

答案 0 :(得分:1)

来自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 APIPHAssetChangeRequestplaceholderForCreatedAsset

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