第三方框架地址
https://github.com/CoderMJLee/MJExtension
MjExtension使用方法
https://github.com/Alamofire/Alamofire
https://github.com/SwiftyJSON/SwiftyJSON
使用方法
cocoapods 安装 http://www.bubuko.com/infodetail-425274.html
Carthage 安装 http://www.mamicode.com/info-detail-905311.html
主要代码
服务器地址请自行更换(主要记录使用方法帮助自己回忆)
上传图片带参数
func senderLocation(phone:NSString!, password:NSString!){
print("phone \(phone) password \(password)")
Alamofire.upload(
.POST,
UploadPhotoUrl,
multipartFormData: { multipartFormData in
//定位地址
multipartFormData.appendBodyPart(data: self.location.dataUsingEncoding(NSUTF8StringEncoding)!, name: "pos")
//图片压缩,转nsdata类型
let imageData = UIImageJPEGRepresentation(self.image!, 0.3)
//获取当前时间格式化成String类型
let date:NSDate = NSDate()
let formatter:NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "yyyyMMddHHmmss"
let dateString = formatter.stringFromDate(date)
//上传图片
multipartFormData.appendBodyPart(data: imageData!, name: "photo", fileName: "\(dateString).jpg", mimeType: "image/jpg")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
self.dismissViewControllerAnimated(true, completion: nil)
case .Failure(let encodingError):
print(encodingError)
}
})
}
获取Json
/**
传入page参数获得数据这个
- parameter page:传入page参数
*/
func getUserInfo(uid:NSString){
Alamofire.request(.GET, GetUserInfoUrl, parameters: ["userNum":uid])
.responseJSON { response in
//使用MJExtension
self.userInfo = UserInfos.mj_objectWithKeyValues(response.result.value!)
if (self.userInfo.code == "success"){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
print("\(ImageUrl)\(self.userInfo.inf!.headImg)")
let data = NSData(contentsOfURL: NSURL(string: "\(ImageUrl)\(self.userInfo.inf!.headImg)")!)
var image = UIImage()
if data != nil{
image = UIImage(data: data!)!
}
dispatch_async(dispatch_get_main_queue(), {
self.photoImage.image = image
})
})
self.tableView.reloadData()
}
}
}
class Inf:NSObject{
var intro:NSString!
var headImg:NSString!
var name:NSString!
var role:NSString!
var department:NSString!
}
class UserInfos: NSObject {
var code:NSString!
var inf:Inf?
}