ios 如何获取 PHAsset 的 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38183613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get URL for a PHAsset?
提问by Utsav Dusad
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?
我正在使用第三方库从照片库中选择多个图像。在选择多个图像时,它返回一组 PHAsset 对象。现在,我想在核心数据中保存这些对象的 URL(或一些参考)。但我不知道如何获取 URL。是否有任何其他参考可以存储在核心数据中可以帮助我从照片库中获取相同的图像?
回答by diegomen
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:
我使用了@iMHitesh Surani 的答案,它工作得很好,我将它转换为 Swift 3.1 并将其放入 PHAsset 的扩展中,以便在任何地方使用该方法,这里是:
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)
}
})
}
}
}