壓縮並用 Alamofire 上傳多張圖片

Util.showGifLoading(self.view) //你的 loading
        let imageDataArray = self.lqPhotoPicker_getBigImageDataArray()//這裏替換成你的圖片 Data 數組
        let imageBigArray = self.lqPhotoPicker_getBigImageArray()//這裏替換成你的 UIImage 數組
        let token:String = UserDefaults.standard.value(forKey: "token") as! String
        let url = "your url"
        let attachArray:NSMutableArray = NSMutableArray()
        for i in 0..<(imageDataArray?.count)! {
            //壓縮圖片
            var imgData = imageDataArray?[i] as! Data
            let imgBig = imageBigArray?[i] as! UIImage
            if(imgData.count/1024 > 100) {
                imgData = UIImageJPEGRepresentation(imgBig,0.1)!  // 壓縮比例在0~1之間,壓縮後視覺上不失真
            }

            Alamofire.upload(
                multipartFormData: { multipartFormData in
                    multipartFormData.append(imgData, withName: "image", fileName: "file.png", mimeType: "image/png")
            },
                to: url,
                encodingCompletion: { encodingResult in
                    switch encodingResult {
                    case .success(let upload, _, _):
                        upload.responseJSON { response in

                            //以下是根據我們後臺返回的數據進行的判斷 請自行調整
                            if let data:NSDictionary = response.result.value as? NSDictionary {
                                if(Util.processServerError(data)) {
                                    if(data.value(forKey: "retCode") as! String != "0"){
                                        //上傳失敗提示
                                        Util.showMessage(self.view, text: (data.value(forKey: "message") as! String))
                                    }else{
                                        attachArray.add((data["url"] as? String)!)
                                        self.attach = attachArray.componentsJoined(by: ",")
                                        if(attachArray.count == imageDataArray?.count) {
                                            //上傳完成
                                        }
                                    }
                                }
                            }else {
                                Util.showMessage(self.view, text: "圖片上傳失敗,請重試") //你的個性化提示
                            }

                        }
                    case .failure(let encodingError):
                        print(encodingError)
                    }
            })
        }


附:壓縮圖片尺寸並壓縮圖片大小

static func zipImage(currentImage: UIImage,scaleSize:CGFloat,percent: CGFloat) -> NSData{  
        //壓縮圖片尺寸  
        UIGraphicsBeginImageContext(CGSizeMake(currentImage.size.width*scaleSize, currentImage.size.height*scaleSize))  
        currentImage.drawInRect(CGRect(x: 0, y: 0, width: currentImage.size.width*scaleSize, height:currentImage.size.height*scaleSize))  
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()  
        UIGraphicsEndImageContext()  
        //高保真壓縮圖片質量  
        //UIImageJPEGRepresentation此方法可將圖片壓縮,但是圖片質量基本不變,第二個參數即圖片質量參數。  
        let imageData: NSData = UIImageJPEGRepresentation(newImage, percent)!  
        return imageData  
    } 



發佈了108 篇原創文章 · 獲贊 76 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章