1. 程式人生 > >[Swift通天遁地]四、網路和執行緒-(9)上傳圖片並實時顯示上傳進度

[Swift通天遁地]四、網路和執行緒-(9)上傳圖片並實時顯示上傳進度

本文將演示上傳圖片至伺服器,並實時顯示上傳的進度。

首先確保在專案中已經安裝了所需的第三方庫。

點選【Podfile】,檢視安裝配置檔案。

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】

現在開始編寫程式碼,實現圖片上傳的功能。

 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 simpleUpload() 12 13 //實時顯示上傳進度的圖片上傳功能 14 complexUpload() 15 } 16 17 //新增一個方法,首先實現一個簡單的上傳動作 18 func simpleUpload() 19 { 20 //獲得專案中圖片所在的路徑 21 let fileURL = Bundle.main.url(forResource: "iphone_large_2x", withExtension: "
jpg") 22 //呼叫網路操作庫的上傳方法,將指定位置的圖片檔案,上傳到伺服器介面。 23 Alamofire.upload(fileURL!,to:"https://httpbin.org/post") 24 .validate() 25 .responseJSON { response in 26 //上傳完成之後,返回主執行緒,彈出上傳結束的提示資訊 27 DispatchQueue.main.async{ 28 //獲得伺服器返回物件的結果資訊 29 let message = "Result:\(response.result)" 30 31 //建立一個警告視窗,並設定彈出視窗的標題、資訊和樣式等屬性 32 let alert = UIAlertController(title: "Information", //標題 33 message: message, //資訊 34 preferredStyle: UIAlertControllerStyle.alert)//樣式 35 //初始化一個警告動作的按鈕控制元件,當點選該按鈕時,關閉彈出視窗。 36 let OKAction = UIAlertAction(title: "OK", 37 style: UIAlertActionStyle.default, 38 handler: nil) 39 //將警告動作新增到視窗中, 40 alert.addAction(OKAction) 41 //然後彈出警告視窗 42 self.present(alert, animated: true, completion: nil) 43 } 44 } 45 } 46 47 //新增一個方法, 48 //實現一個可實時顯示上傳進度的圖片上傳功能 49 func complexUpload() 50 { 51 //獲得專案中圖片所在的路徑 52 let fileURL = Bundle.main.url(forResource: "iphone_large_2x", withExtension: "jpg") 53 //呼叫網路操作庫的上傳方法,將指定位置的圖片檔案,上傳到伺服器介面。 54 Alamofire.upload(fileURL!,to:"https://httpbin.org/post") 55 //在上傳進度的方法中,處理返回的進度資訊 56 .uploadProgress { progress in 57 58 //在控制檯輸出:上傳進度的完成比例 59 print("---fractionCompleted:\(progress.fractionCompleted)") 60 //在控制檯輸出:已經上傳的位元組數 61 print("---completedUnitCount:\(progress.completedUnitCount)") 62 //在控制檯輸出:總的位元組數 63 print("---totalUnitCount:\(progress.totalUnitCount)") 64 } 65 //呼叫驗證方法,驗證上傳的任務 66 .validate() 67 //處理上傳結束後,伺服器返回的資料 68 .responseJSON { response in 69 //上傳完成之後,返回主執行緒,彈出上傳結束的提示資訊 70 DispatchQueue.main.async{ 71 //在控制檯輸出:伺服器返回物件的結果資訊 72 print("---Result:\(response.result)") 73 //獲得伺服器返回物件的結果資訊 74 let message = "Result:\(response.result)" 75 76 //建立一個警告視窗,並設定彈出視窗的標題、資訊和樣式等屬性 77 let alert = UIAlertController(title: "Information", //標題 78 message: message,//資訊 79 preferredStyle: UIAlertControllerStyle.alert)//樣式 80 //初始化一個警告動作的按鈕控制元件,當點選該按鈕時,關閉彈出視窗。 81 let OKAction = UIAlertAction(title: "OK", 82 style: UIAlertActionStyle.default, 83 handler: nil) 84 //將警告視窗新增到視窗中 85 alert.addAction(OKAction) 86 //然後彈出警告視窗 87 self.present(alert, animated: true, completion: nil) 88 } 89 } 90 } 91 92 93 override func didReceiveMemoryWarning() { 94 super.didReceiveMemoryWarning() 95 // Dispose of any resources that can be recreated. 96 } 97 }