我想上傳的UIView(圖),但我在郵件接收空圖片(來自Web服務)... Android是工作上傳的UIImage base64編碼
UIGraphicsBeginImageContext(self.view.bounds.size);
self.view.layer.render(in: UIGraphicsGetCurrentContext()!)
let screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
let jpegCompressionQuality: CGFloat = 1
let imgData:Data = UIImageJPEGRepresentation(screenShot!,jpegCompressionQuality)!
let lengthOfData = imgData.count
let strBase64 = imgData.base64EncodedString(options: [])
var request = URLRequest(url: URL(string: "http://myws.com/Chart.php")!)
request.httpMethod = "POST"
let postString = "data=\(strBase64)&[email protected]"
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("\(lengthOfData)", forHTTPHeaderField: "Content-Length")
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
我認爲你在這裏錯過了一些東西,如爲httpheaderfield設置值等 – KrishnaCA