How to download video from url and save it in to document directory in iOS?

前端 未结 4 1870
旧巷少年郎
旧巷少年郎 2021-01-03 12:57

How to download video from url and save it in to document directory in iOS

4条回答
  •  醉酒成梦
    2021-01-03 13:13

    You can also implement same in Swift 2.0

    class func downloadVideo(videoImageUrl:String)
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
            //All stuff here
    
            let url=NSURL(string: videoImageUrl)
            let urlData=NSData(contentsOfURL: url!)
    
            if((urlData) != nil)
            {
                let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
    
                let fileName = videoImageUrl.lastPathComponent //.stringByDeletingPathExtension
    
                let filePath="\(documentsPath)/\(fileName)"
    
                //saving is done on main thread
    
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
    
                     urlData?.writeToFile(filePath, atomically: true)
                })
    
            }
        })
    
    }
    

提交回复
热议问题