swift下载文件并且保存到沙盒

前端之家收集整理的这篇文章主要介绍了swift下载文件并且保存到沙盒前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

func downimage(){

var urlStr:NSString ="http://www.sinaimg.cn/dy/slidenews/4_img/2016_19/704_1916886_343904.jpg"

var request:NSURLRequest =NSURLRequest(URL: NSURL(string: urlStr as String)!,cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData,timeoutInterval:2.0)

NSURLConnection.sendAsynchronousRequest(request,queue:NSOperationQueue()) { (response,data,error) ->Void in

var path:NSString =NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory,NSSearchPathDomainMask.UserDomainMask,true)[0];

var cachepaht = path.stringByAppendingPathComponent("test.jpg")

data?.writeToFile(cachepaht,atomically:true);

}

}

猜你在找的Swift相关文章