无法使用 Swift 上传图片
Unable to upload an image using Swift
我无法使用 swift 上传图片。所有其他信息都正确插入到数据库中。此功能提交书籍详细信息,但还需要提交图像。如何使用我当前的 swift 代码提交图片?谢谢。
func uploadOrder (completion: @escaping (Bool, Any?, Error?) -> Void) {
let imageSize: CGSize = (self._book.imageView?.bounds.size)!
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
let context = UIGraphicsGetCurrentContext()
self._book.imageView?.layer .render(in: context!)
let myImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
let imageData: NSData = UIImageJPEGRepresentation(myImage, 90)! as NSData
UIGraphicsEndImageContext()
let jsonDictionary = NSMutableDictionary()
guard let url = URL(string: Constants.uploadOrder) else { return }
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
if let isbn = self._book.ISBN as String? {
jsonDictionary.setValue(isbn, forKey:"isbn")
} else {
jsonDictionary.setValue("", forKey:"isbn")
}
guard let httpBody = try? JSONSerialization.data(withJSONObject: jsonDictionary, options: []) else {
return
}
urlRequest.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: urlRequest) { (data, response, error) in
if let response = response {
print("Response", response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
if let status = (json as AnyObject).value(forKey:"OK") as! Bool? {
DispatchQueue.main.async { //GUI thread
completion(true, status, nil)
}
}
} catch let error {
print(error.localizedDescription)
DispatchQueue.main.async { //GUI thread
completion(false, nil, error)
}
}
}
}.resume()
}
您可以将图像数据转换为 base64
let base64String = imageData!.base64EncodedString(options: .lineLength64Characters)
并将其作为键的值添加到您的 jsonDictionary
jsonDictionary.setValue(base64String, forKey:"image")
我无法使用 swift 上传图片。所有其他信息都正确插入到数据库中。此功能提交书籍详细信息,但还需要提交图像。如何使用我当前的 swift 代码提交图片?谢谢。
func uploadOrder (completion: @escaping (Bool, Any?, Error?) -> Void) {
let imageSize: CGSize = (self._book.imageView?.bounds.size)!
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
let context = UIGraphicsGetCurrentContext()
self._book.imageView?.layer .render(in: context!)
let myImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
let imageData: NSData = UIImageJPEGRepresentation(myImage, 90)! as NSData
UIGraphicsEndImageContext()
let jsonDictionary = NSMutableDictionary()
guard let url = URL(string: Constants.uploadOrder) else { return }
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
if let isbn = self._book.ISBN as String? {
jsonDictionary.setValue(isbn, forKey:"isbn")
} else {
jsonDictionary.setValue("", forKey:"isbn")
}
guard let httpBody = try? JSONSerialization.data(withJSONObject: jsonDictionary, options: []) else {
return
}
urlRequest.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: urlRequest) { (data, response, error) in
if let response = response {
print("Response", response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
if let status = (json as AnyObject).value(forKey:"OK") as! Bool? {
DispatchQueue.main.async { //GUI thread
completion(true, status, nil)
}
}
} catch let error {
print(error.localizedDescription)
DispatchQueue.main.async { //GUI thread
completion(false, nil, error)
}
}
}
}.resume()
}
您可以将图像数据转换为 base64
let base64String = imageData!.base64EncodedString(options: .lineLength64Characters)
并将其作为键的值添加到您的 jsonDictionary
jsonDictionary.setValue(base64String, forKey:"image")