如何获取PHAsset的URL? [英] How to get URL for a PHAsset?
问题描述
我正在使用第三方库从照片库中选择多个图像。在选择多个图像时,它返回一组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?
推荐答案
我使用了Hitesh surani的答案,它完美无缺,我将其转换为Swift 3.1并将其放入PHAsset的扩展中为了在任何地方使用该方法,这里是:
I used Hitesh 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屋!