您好,登錄后才能下訂單哦!
import Foundation
import Alamofire
//上傳圖片 ,multipartFormData 上傳。key = attach
extension HttpManager {
/**
上傳單張圖片
- parameter p_w_picpath: UIImage
- parameter success: 成功回調(diào)圖片 model
- parameter failure: 失敗
*/
class func uploadSingleImage(
_ p_w_picpath:UIImage,
success:@escaping (_ p_w_picpathModel: UploadImageModel) ->Void,
failure:@escaping (Void) ->Void)
{
let parameters = [
"access_token": UserInstance.accessToken
]
let p_w_picpathData = UIImageJPEGRepresentation(p_w_picpath, 0.7)
/*
這里需要填寫上傳圖片的 API
*/
let uploadIImageURLString = ""
Alamofire.upload(
multipartFormData: { multipartFormData in
if p_w_picpathData != nil {
multipartFormData.append(p_w_picpathData!, withName: "attach", fileName: "file", mimeType: "p_w_picpath/jpeg")
}
for (key, value) in parameters {
multipartFormData.append(value!.data(using: String.Encoding.utf8)!, withName: key)
}
},
to: uploadIImageURLString,
encodingCompletion: { result in
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
switch response.result {
case .success(let data):
/*
根據(jù) JSON 返回格式,做好 UploadImageModel 的 key->value 映射, 這里只是個例子
*/
let model: UploadImageModel = TSMapper<UploadImageModel>().map(JSONObject:data)!
success(model)
case .failure( _):
failure()
}
}
case .failure(let encodingError):
debugPrint(encodingError)
}
})
}
/**
上傳多張圖片
- parameter p_w_picpathsArray: 圖片數(shù)組
- parameter success: 返回圖片數(shù)組 model,和圖片逗號分割的字符串 "123123,23344,590202"
- parameter failure: 失敗
*/
class func uploadMultipleImages(
_ p_w_picpathsArray:[UIImage],
success:@escaping (_ p_w_picpathModel: [UploadImageModel], _ p_w_picpathsId: String) ->Void,
failure:@escaping (Void) ->Void)
{
guard p_w_picpathsArray.count != 0 else {
assert(p_w_picpathsArray.count == 0, "Invalid p_w_picpaths array") // here
failure()
return
}
for p_w_picpath in p_w_picpathsArray {
guard p_w_picpath.isKind(of: UIImage.self) else {
failure()
return
}
}
let resultImageIdArray = NSMutableArray()
let resultImageModelArray = NSMutableArray()
let emtpyId = ""
for _ in 0..<p_w_picpathsArray.count {
resultImageIdArray.add(emtpyId)
}
let group = DispatchGroup()
var index = 0
for (p_w_picpath) in p_w_picpathsArray {
group.enter();
self.uploadSingleImage(
p_w_picpath,
success: {model in
let p_w_picpathId = model.p_w_picpathId
resultImageIdArray.replaceObject(at: index, with: p_w_picpathId!)
resultImageModelArray.add(model)
group.leave();
},
failure: {
group.leave();
}
)
index += 1
}
group.notify(queue: DispatchQueue.main, execute: {
let checkIds = resultImageIdArray as NSArray as! [String]
for p_w_picpathId: String in checkIds {
if p_w_picpathId == emtpyId {
failure()
return
}
}
let ids = resultImageIdArray.componentsJoined(by: ",")
let p_w_picpaths = resultImageModelArray as NSArray as! [UploadImageModel]
success(p_w_picpaths, ids)
})
}
}
//
//import UIKit
//
////簡寫的 key->value
//public typealias ts_parameters = [String : AnyObject]
//public typealias SuccessClosure = (AnyObject) -> Void
//public typealias FailureClosure = (NSError) -> Void
//
//class HttpManager: NSObject {
// class var sharedInstance : HttpManager {
// struct Static {
// static let instance : HttpManager = HttpManager()
// }
// return Static.instance
// }
//
// fileprivate override init() {
// super.init()
// }
//}
//
//import Foundation
//import ObjectMapper
//
//// 上傳圖片接口返回的圖片 Model
//class UploadImageModel : TSModelProtocol {
// var originalURL : String?
// var originalHeight : CGFloat?
// var originalWidth : CGFloat?
// var thumbURL : String?
// var thumbHeight : CGFloat?
// var thumbWidth : CGFloat?
// var p_w_picpathId : Int?
//
// required init?(map: Map) {
//
// }
//
// func mapping(map: Map) {
// originalURL <- map["original_URL"]
// originalHeight <- map["original_height"]
// originalWidth <- map["original_width"]
// p_w_picpathId <- map["p_w_picpath_Id"]
// thumbURL <- map["thumb_URL"]
// thumbHeight <- map["thumb_height"]
// thumbWidth <- map["thumb_width"]
// }
//}
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。