guard let img = UIImage(named: "aliyun-logo-local") else {
return
}
let imgData = UIImageJPEGRepresentation(img,1)
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0]
let imgPath = "\(path)/\("aliyun-logo-local.png")"
NSData(data: imgData!).write(toFile: imgPath,atomically: true)
let imgView = UIImageView()
view.addSubview(imgView)
imgView.frame = CGRect(x: 100,y: 100,width: 100,height: 100)
DispatchQueue.global().async(execute: {
let fileImage = UIImage.init(contentsOfFile: imgPath)
DispatchQueue.main.async(execute: {
imgView.image = fileImage
})
})
//let fileURL = Bundle.main.url(forResource: "aliyun-logo-local",withExtension: "png")
let searchURL : URL = URL(fileURLWithPath: imgPath)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(searchURL,withName: "file")
},to: "http://127.0.0.1/upload/upload_file.PHP",encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload,_,_):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
})
原文链接:https://www.f2er.com/swift/322739.html