采得百花成蜜后,为谁辛苦为谁甜。这篇文章主要讲述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 = https://www.songbingjia.com/android/UIImageJPEGRepresentation(image, 0.7)
/*
这里需要填写上传图片的 API
*/
【Alamofire +ObjectMapper模型: 上传单张图片,上传多张图片。】
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"]
//
}
//}
推荐阅读
- Android -- 贝塞尔实现水波纹动画(划重点!!)
- word 2010 样式默认值的设置_Word专区
- word 2010 运用文本框完成纵横混排_Word专区
- WPS Office与微软Office精确兼容 免费办公软件便捷实用_WPS office
- 简单打开WPS 灵活运用WINDOWS快捷键_WPS office
- WPS Office 2012界面风格切换轻巧便捷_WPS office
- WPS Office 2012全攻略 技巧所有方面加强_WPS office
- WPS文档要怎样新建与保存_WPS office
- WPS多文档标签编辑设置大局部设置_WPS office