Skip to content

Instantly share code, notes, and snippets.

@amos-yau
Created July 7, 2018 03:19
Show Gist options
  • Save amos-yau/d9a4e9a08f3770c2cb35dceeaad42966 to your computer and use it in GitHub Desktop.
Save amos-yau/d9a4e9a08f3770c2cb35dceeaad42966 to your computer and use it in GitHub Desktop.
get URL for a PHAsset
import Photos
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)
}
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment