01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | / / HTTP / HTTPS / / URL : [ url ]http : / / pic.baike.soso.com / p / 20120424 / bki -20120424022918 -1539307851. jpg[ / url ] / / 创建一个URL对象 var url = NSURL ( string : "https://cache.yisu.com/upload/information/20200311/47/203912.jpg" ) / / 创建一个HTTP请求 var request = NSURLRequest ( URL : url ) / / 发送同步请求,开始下载数据 var response : NSURLResponse? var error : NSError? var data = NSURLConnection.sendSynchronousRequest ( request , returningResponse : & response , error : & error ) println ( "\(response):\(error)" ) var httpResponse = response as NSHTTPURLResponse / / 数据下载完成 if httpResponse.statusCode = = 200 { println ( "下载了\(data.length)字节" ) data .writeToFile ( "/Users/cheetah/Desktop/test.jpg" , atomically : true ) } else { println ( "下载失败: \(httpResponse.statusCode)" ) } |
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。