iOS : How to convert a video PHAsset to NSData

testa abalez picture testa abalez · Aug 14, 2015 · Viewed 13.9k times · Source

I want to put the videos stored on my iPhone to my Google Drive. I have already done with images, but with videos, it's an other problem...

For images, no problem, I convert my asset to an NSData with this method :

   data = UIImagePNGRepresentation(result!)!

And I put the image to my drive !

But, for videos, I tried many different ways, but no, I can't.

How can I do ?

Thanks a lot !

Answer

testa abalez picture testa abalez · Aug 14, 2015

I did it !

This is the solution :

    PHCachingImageManager().requestAVAssetForVideo(asset, options: nil, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [NSObject : AnyObject]?) in
        dispatch_async(dispatch_get_main_queue(), {

            let asset = asset as? AVURLAsset
            var data = NSData(contentsOfURL: asset.URL)
    })
})

And after, you have the good NSData variable which you can use to put your video to the Cloud !