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!")
}
}
}
}
}
}