0

I'm trying on click of a button to download a video into my phone.

I"m using the following:

@IBAction func startDownload(_ sender: AnyObject) {
    let videoImageUrl = "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"

    dispatch_async.global(DispatchQueue.(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
        let url = NSURL(string: videoImageUrl);
        let urlData = NSData(contentsOfURL: url! as URL);
        if(urlData != nil)
        {
            let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .userDomainMask, true)[0];
            let filePath="\(documentsPath)/tempFile.mp4";
            dispatch_async(DispatchQueue.main, {
                urlData?.writeToFile(filePath, atomically: true);
                PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                    PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(fileURLWithPath: filePath))
                }) { completed, error in
                    if completed {
                        print("Video is saved!")
                    }
                }
            })
        }
    })

}

without any success.

I'm having a 'Cannot invoke 'global' with an argument list of type (Int, Int)'

and unresolved identifier for PHPhotoLIbrary and PHAssetChangeRequest

Does anybody would know how can this work ?

Thanks a lot :)

-- EDIT --

@IBAction func startDownload(_ sender: UIButton) {
    let videoImageUrl = "https://my-video.mp4"


    DispatchQueue.global(qos: .default).async {
        let url = NSURL(string: videoImageUrl);
        let urlData = NSData(contentsOf: url! as URL);
        if(urlData != nil)
        {
            let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];
            let filePath="\(documentsPath)/tempFile.mp4";
            DispatchQueue.main.async {
                urlData?.write(toFile: filePath, atomically: true);
                PHPhotoLibrary.shared().performChanges({
                    PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: NSURL(fileURLWithPath: filePath) as URL)
                }) { completed, error in
                    if completed {
                        print("Video is saved!")
                    }
                }
            }
        }
    }
}
tibewww
  • 593
  • 4
  • 11
  • 32

1 Answers1

0

There are several things wrong with your code.

First the way you use dispatch queues. The right way for a background queue is:

DispatchQueue.global(qos: .default).async {
    // Code ...
}

or for the main queue:

DispatchQueue.main.async {
    // Code ...
}

Second, in order to use the Photos framework, you need to import it. Add import Photos at the top of your file.

Third, there are several API changes in Swift 3, but the compiler should offer to auto-fix them (for example you should use URL and Data everywhere, not NSURL or NSData). Alternatively, you can convert a Swift 2 project into a Swift 3 project by going to Edit -> Convert -> To Current Swift Syntax

And last, the way you download your video is wrong, it will be entirely loaded in memory before being saved to disk. If the video is big this will cause issues. Look into the URLSession API for a way to download a ressource directly into a file.

deadbeef
  • 5,409
  • 2
  • 17
  • 47
  • Hi Thanks a lot, code is working ! Otherwise, when I click on the button download, after a whie nothing change - and the app get frozen . . is id du to the URLSession API ?? I'd like the video to be stock in the App - not in the photo library, just to be able to play it after that from where it is temporary stored . .. is there a way to achieve that ? – tibewww Feb 24 '17 at 13:47