Last active
June 18, 2019 22:55
-
-
Save narendra-r/51bfe4f0c8b45dbeee040193cc6874cf to your computer and use it in GitHub Desktop.
Read video data from video URl in Swift
This file contains hidden or 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 | |
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