Created
July 7, 2018 03:19
-
-
Save amos-yau/d9a4e9a08f3770c2cb35dceeaad42966 to your computer and use it in GitHub Desktop.
get URL for a PHAsset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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