11

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 !

testa abalez
  • 1,042
  • 3
  • 13
  • 28
  • what format is the file (`.mp4`)? Check out the answer [in this related question and let me know if this helps you out](http://stackoverflow.com/a/19565488/981049). – Michael Dautermann Aug 14 '15 at 07:51
  • I don't know, but it's the videos stored on my iPhone which I create with the camera of the iPhone, it's probably .mov I think. – testa abalez Aug 14 '15 at 07:52

4 Answers4

25

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 !

testa abalez
  • 1,042
  • 3
  • 13
  • 28
  • It dosen't work.The correct solution is [how-to-get-nsdata-from-file-by-using-phasset](http://stackoverflow.com/questions/35652094/how-to-get-nsdata-from-file-by-using-phasset) – Xingxing Dec 27 '16 at 08:51
1

Please add Bellow solution its work for me

if you miss option.isNetworkAccessAllowed = true then you get error for genera the url

private let options: PHVideoRequestOptions = PHVideoRequestOptions()

option.isNetworkAccessAllowed = true 

PHImageManager.default().requestAVAsset(-------
Pradeep
  • 9,667
  • 13
  • 27
  • 34
Raj
  • 11
  • 2
1

Updated for Swift 5

PHImageManager or PHCachingImageManager can be used here

PHImageManager.default().requestAVAsset(forVideo: asset,
                                        options: nil) { (asset, audioMix, info) in
     if 
         let asset = asset as? AVURLAsset,
         let data = NSData(contentsOf: asset.url) {
             //do smth with data
         }
      }
                                                        
}
Raman Shyniauski
  • 392
  • 2
  • 13
0

Fetch synchronously Image/Video Swift 5 + caching

extension PHAsset {
    func getImage() -> UIImage? {
        let manager = PHCachingImageManager.default
        let option = PHImageRequestOptions()
        option.isSynchronous = true
        var img: UIImage? = nil
        manager().requestImage(for: self, targetSize: CGSize(width: self.pixelWidth, height: self.pixelHeight), contentMode: .aspectFit, options: nil, resultHandler: {(result, info) -> Void in
            img = result!
        })
        return img
    }

    func getVideo() -> NSData? {
        let manager = PHCachingImageManager.default
        let option = PHImageRequestOptions()
        option.isSynchronous = true
        var resultData: NSData? = nil
    
        manager().requestAVAsset(forVideo: self, options: nil) { (asset, audioMix, info) in
            if let asset = asset as? AVURLAsset, let data = NSData(contentsOf: asset.url) {
                resultData = data
            }
        }
        return resultData
    }
}
K_Mohit
  • 528
  • 3
  • 17