首页 > 代码库 > Alamofire +ObjectMapper模型: 上传单张图片,上传多张图片。
Alamofire +ObjectMapper模型: 上传单张图片,上传多张图片。
import Foundation
import Alamofire
//上传图片 ,multipartFormData 上传。key = attach
extension HttpManager {
/**
上传单张图片
- parameter image: UIImage
- parameter success: 成功回调图片 model
- parameter failure: 失败
*/
class func uploadSingleImage(
_ image:UIImage,
success:@escaping (_ imageModel: UploadImageModel) ->Void,
failure:@escaping (Void) ->Void)
{
let parameters = [
"access_token": UserInstance.accessToken
]
let imageData = http://www.mamicode.com/UIImageJPEGRepresentation(image, 0.7)
/*
这里需要填写上传图片的 API
*/
let uploadIImageURLString = ""
Alamofire.upload(
multipartFormData: { multipartFormData in
if imageData != nil {
multipartFormData.append(imageData!, withName: "attach", fileName: "file", mimeType: "image/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 imagesArray: 图片数组
- parameter success: 返回图片数组 model,和图片逗号分割的字符串 "123123,23344,590202"
- parameter failure: 失败
*/
class func uploadMultipleImages(
_ imagesArray:[UIImage],
success:@escaping (_ imageModel: [UploadImageModel], _ imagesId: String) ->Void,
failure:@escaping (Void) ->Void)
{
guard imagesArray.count != 0 else {
assert(imagesArray.count == 0, "Invalid images array") // here
failure()
return
}
for image in imagesArray {
guard image.isKind(of: UIImage.self) else {
failure()
return
}
}
let resultImageIdArray = NSMutableArray()
let resultImageModelArray = NSMutableArray()
let emtpyId = ""
for _ in 0..<imagesArray.count {
resultImageIdArray.add(emtpyId)
}
let group = DispatchGroup()
var index = 0
for (image) in imagesArray {
group.enter();
self.uploadSingleImage(
image,
success: {model in
let imageId = model.imageId
resultImageIdArray.replaceObject(at: index, with: imageId!)
resultImageModelArray.add(model)
group.leave();
},
failure: {
group.leave();
}
)
index += 1
}
group.notify(queue: DispatchQueue.main, execute: {
let checkIds = resultImageIdArray as NSArray as! [String]
for imageId: String in checkIds {
if imageId == emtpyId {
failure()
return
}
}
let ids = resultImageIdArray.componentsJoined(by: ",")
let images = resultImageModelArray as NSArray as! [UploadImageModel]
success(images, 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 imageId : Int?
//
// required init?(map: Map) {
//
// }
//
// func mapping(map: Map) {
// originalURL <- map["original_URL"]
// originalHeight <- map["original_height"]
// originalWidth <- map["original_width"]
// imageId <- map["image_Id"]
// thumbURL <- map["thumb_URL"]
// thumbHeight <- map["thumb_height"]
// thumbWidth <- map["thumb_width"]
// }
//}
Alamofire +ObjectMapper模型: 上传单张图片,上传多张图片。