您好,登录后才能下订单哦!
import Foundation
import Alamofire
//上传图片 ,multipartFormData 上传。key = attach
extension HttpManager {
/**
上传单张图片
- parameter p_w_picpath: UIImage
- parameter success: 成功回调图片 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):
/*
根据 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: 图片数组
- parameter success: 返回图片数组 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"]
// }
//}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。