Skip to content

Instantly share code, notes, and snippets.

@narendra-r
Last active June 18, 2019 22:55
Show Gist options
  • Save narendra-r/51bfe4f0c8b45dbeee040193cc6874cf to your computer and use it in GitHub Desktop.
Save narendra-r/51bfe4f0c8b45dbeee040193cc6874cf to your computer and use it in GitHub Desktop.
Read video data from video URl in Swift
import Photos
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
self.dismissViewControllerAnimated(true, completion: nil)
if let referenceURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([referenceURL], options: nil)
if let phAsset = fetchResult.firstObject as? PHAsset {
PHImageManager.defaultManager().requestAVAssetForVideo(phAsset, options: PHVideoRequestOptions(), resultHandler: { (asset, audioMix, info) -> Void in
if let asset = asset as? AVURLAsset {
let videoData = NSData(contentsOfURL: asset.URL)
// optionally, write the video to the temp directory
let videoPath = NSTemporaryDirectory() + "tmpMovie.MOV"
let videoURL = NSURL(fileURLWithPath: videoPath)
let writeResult = videoData?.writeToURL(videoURL, atomically: true)
if let writeResult = writeResult where writeResult {
print("success")
}
else {
print("failure")
}
}
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment