如何获取 PHAsset 的 URL? [英] How to get URL for a PHAsset?

查看:153
本文介绍了如何获取 PHAsset 的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第三方库从照片库中选择多个图像.在选择多个图像时,它返回一组 PHAsset 对象.现在,我想在核心数据中保存这些对象的 URL(或一些参考).但我不知道如何获取 URL.是否有任何其他参考可以存储在核心数据中可以帮助我从照片库中获取相同的图像?

I am using a third party library to select multiple images from the Photo Library. On selecting multiple images it returns an array of PHAsset objects. Now, I want to save the URL (or some reference) for these objects in the core data. But I do not know how to get the URL. Is there any other reference I could store in the core data which could help me in fetching the same image from the photo library?

推荐答案

我使用了 @iMHitesh Surani 回答和它运行良好,我将其转换为 Swift 3.1 并将其放入 PHAsset 的扩展中,以便在任何地方使用该方法,这里是:

I used @iMHitesh Surani answer and it worked perfect, I converted it into Swift 3.1 and put it into an extension of PHAsset in order to use that method everywhere, here it is:

extension PHAsset {

    func getURL(completionHandler : @escaping ((_ responseURL : URL?) -> Void)){
        if self.mediaType == .image {
            let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
            options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
                return true
            }
            self.requestContentEditingInput(with: options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [AnyHashable : Any]) -> Void in
                completionHandler(contentEditingInput!.fullSizeImageURL as URL?)
            })
        } else if self.mediaType == .video {
            let options: PHVideoRequestOptions = PHVideoRequestOptions()
            options.version = .original
            PHImageManager.default().requestAVAsset(forVideo: self, options: options, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [AnyHashable : Any]?) -> Void in
                if let urlAsset = asset as? AVURLAsset {
                    let localVideoUrl: URL = urlAsset.url as URL
                    completionHandler(localVideoUrl)
                } else {
                    completionHandler(nil)
                }
            })
        }
    }
}

这篇关于如何获取 PHAsset 的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆