★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-wyggofbd-kw.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
目錄:[Swift]通天遁地Swiftios
本文將演示下載網絡圖片,顯示下載進度,並保存在沙箱目錄中。git
首先確保在項目中已經安裝了所需的第三方庫。github
點擊【Podfile】,查看安裝配置文件。swift
1 source 'https://github.com/CocoaPods/Specs.git' 2 platform :ios, '12.0' 3 use_frameworks! 4 5 target ‘DemoApp’ do 6 pod 'Alamofire', '~> 4.0' 7 end
根據配置文件中的相關配置,安裝第三方庫。服務器
而後點擊打開【DemoApp.xcworkspace】項目文件。微信
在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】網絡
如今開始編寫代碼,實現一個簡單的功能:下載一個文件,並將該文件存儲在沙箱中。app
1 import UIKit 2 //在當前的類文件中,引入已經安裝的第三方類庫 3 import Alamofire 4 5 class ViewController: UIViewController { 6 //視圖加載完成的方法 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 // Do any additional setup after loading the view, typically from a nib. 10 //簡單的圖片下載功能 11 simpleDownload() 12 13 //實時顯示下載進度的圖片下載功能 14 complexDownload() 15 } 16 17 //添加一個方法, 18 //經過訪問服務器的接口,下載並保存一個文本文件 19 func simpleDownload() 20 { 21 //初始化一個下載請求對象,並設置下載後的存儲目錄,位於沙箱的文檔目錄中。 22 let destination = DownloadRequest.suggestedDownloadDestination( 23 for: .documentDirectory, 24 in: .userDomainMask 25 ) 26 //在控制檯輸出沙箱中的文檔目錄, 27 //使用這條日誌信息,進入該目錄並查看下載後的文件。 28 print(NSHomeDirectory()+"/Documents") 29 30 //調用網絡操做庫的下載方法,下載指定的服務器接口, 31 //並將下載後的文件存儲在指定的目錄中。 32 let result = Alamofire.download("https://httpbin.org/stream/1", to: destination) 33 //控制檯輸出結果 34 print(result) 35 } 36 37 //添加一個方法, 38 //實現一個可實時顯示下載進度的圖片下載功能 39 func complexDownload() 40 { 41 //初始化一個下載請求對象,並設置下載後的存儲目錄,位於沙箱的文檔目錄中。 42 let destination = DownloadRequest.suggestedDownloadDestination( 43 for: .documentDirectory, 44 in: .userDomainMask 45 ) 46 47 //還能夠設置服務器接口的請求參數 48 let parameters: Parameters = ["foo": "bar"] 49 50 //調用網絡操做庫的下載方法,下載指定的服務器接口, 51 //並將下載後的文件存儲在指定的目錄中。 52 Alamofire.download("http://images.apple.com/v/home/cx/images/gallery/iphone_large_2x.jpg", 53 method: .get, 54 parameters: parameters, 55 encoding: JSONEncoding.default, 56 to: destination) 57 //經過一個下載進度方法塊,處理圖片下載的事件 58 .downloadProgress(queue: DispatchQueue.global()) 59 { 60 progress in 61 //在控制檯輸出:進度的完成比例 62 print("Progress1: \(progress.fractionCompleted)") 63 //在控制檯輸出:已經下載的字節數 64 print("Progress2: \(progress.completedUnitCount)") 65 //在控制檯輸出:所有的字節數 66 print("Progress3: \(progress.totalUnitCount)") 67 } 68 //完成的圖片的下載後執行驗證方法,此處返回成功標識 69 .validate { request, response, temporaryURL, destinationURL in 70 return .success 71 } 72 //處理圖片下載後的返回信息 73 .responseJSON { response in 74 //在控制檯輸出:網絡返回對象 75 debugPrint("response:\(response)") 76 //在控制檯輸出:圖片下載臨時存放的位置 77 print("response.temporaryURL:\(response.temporaryURL)") 78 //在控制檯輸出:下載後的存儲位置 79 print("response.destinationURL:\(response.destinationURL)") 80 } 81 } 82 83 override func didReceiveMemoryWarning() { 84 super.didReceiveMemoryWarning() 85 // Dispose of any resources that can be recreated. 86 } 87 }