如何将nil图片作为Swift数据类型发送到Swift中的REST服务



我正在尝试通过REST Service上传图片(使用FTP方式(。所以我有一个如下的函数:

func saveFeedBack(feedbackType: String, feedbackText: String, picture1: Data, picture2: Data, picture3: Data)
{
let parameters = [
"ofeedbackType": feedbackType,
"ofeedbackText": feedbackText
]
let oimage1 = UIImage(data: picture1)
let oimage2 = UIImage(data: picture2)
let oimage3 = UIImage(data: picture3)
guard let mediaImage1 = Media(withImage: oimage1!, forKey: "pic1") else { return }
guard let mediaImage2 = Media(withImage: oimage2!, forKey: "pic2") else { return }
guard let mediaImage3 = Media(withImage: oimage3!, forKey: "pic3") else { return }
guard let url = URL(string: "http://abc.def.com/Mobile/SSS/feedback") else { return }
var request = URLRequest(url: url)

request.httpMethod = "POST"
let boundary = generateBoundary()
request.setValue("multipart/form-data; boundary=(boundary)", forHTTPHeaderField: "Content-Type")
let dataBody = createDataBody(withParameters: parameters, media: [mediaImage1, mediaImage2, mediaImage3], boundary: boundary)
request.httpBody = dataBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data
{
do
{
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any]
{
//some code in here
}
}
catch
{
print(error)
}
}
}.resume()
}

这是介质结构:

import UIKit
struct Media {
let key: String
let filename: String
let data: Data
let mimeType: String
init?(withImage image: UIImage, forKey key: String) {
self.key = key
self.mimeType = "image/jpeg"
self.filename = "fotograf.jpg"
guard let data = image.jpegData(compressionQuality: 1) else { return nil }
self.data = data
}
}

让我们讨论一下这个场景:假设我正在发送picture 1picture 2。但我不会发送picture 3。当然,由于这个代码,我得到了以下错误:guard let mediaImage3 = Media(withImage: oimage3!, forKey: "pic3") else { return }

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

如何解决上述问题?图片应该是可选的。我的意思是:如果我不发送任何图片,功能应该可以正常工作

以下是CreateDataBody函数

func createDataBody(withParameters params: Parameters?, media: [Media]?, boundary: String) -> Data {
let lineBreak = "rn"
var body = Data()
if let parameters = params {
for (key, value) in parameters {
body.append("--(boundary + lineBreak)")
body.append("Content-Disposition: form-data; name="(key)"(lineBreak + lineBreak)")
body.append("(value + lineBreak)")
}
}
if let media = media {
for photo in media {
body.append("--(boundary + lineBreak)")
body.append("Content-Disposition: form-data; name="(photo.key)"; filename="(photo.filename)"(lineBreak)")
body.append("Content-Type: (photo.mimeType + lineBreak + lineBreak)")
body.append(photo.data)
body.append(lineBreak)
}
}
body.append("--(boundary)--(lineBreak)")
return body
}

你能试试这个吗?

import UIKit
struct Media {
let key: String
let filename: String
let data: Data?
let mimeType: String
init?(withImage image: UIImage, forKey key: String) {
self.key = key
self.mimeType = "image/jpeg"
self.filename = "photo.jpg"
self.data = image.jpegData(compressionQuality: 1)
}  
}
func createDataBody(withParameters params: Parameters?, media: [Media]?, boundary: String) -> Data {
let lineBreak = "rn"
var body = Data()
if let parameters = params {
for (key, value) in parameters {
body.append("--(boundary + lineBreak)")
body.append("Content-Disposition: form-data; name="(key)"(lineBreak + lineBreak)")
body.append("(value + lineBreak)")
}
}
if media != nil {
print("media cnt: ", media!.count)
if let media = media {
for photo in media {
body.append("--(boundary + lineBreak)")
body.append("Content-Disposition: form-data; name="(photo.key)"; filename="(photo.filename)"(lineBreak)")
body.append("Content-Type: (photo.mimeType + lineBreak + lineBreak)")
body.append(photo.data!)
body.append(lineBreak)
print("photo.key", photo.key)
}
}
}

body.append("--(boundary)--(lineBreak)")
return body
}
func saveFeedBack(feedbackType: String, feedbackText: String, picture1: Data, picture2: Data, picture3: Data)
{
var imageList : [Media] = []
let parameters = [
"ofeedbackType": feedbackType,
"ofeedbackText": feedbackText
]

if  let img1 = UIImage(data: picture1) {
if img1.size.width > 0 {
if let oImg1 = Media(withImage: img1, forKey: "pic1") {
imageListe.append(oImg1)
}
}
}
if  let img2 = UIImage(data: picture2) {
if img2.size.width > 0 {
if let oImg2 = Media(withImage: img2, forKey: "pic2") {
imageListe.append(oImg2)
}
}
}
if  let img3 = UIImage(data: picture3) {
if img3.size.width > 0 {
if let oImg3 = Media(withImage: img3, forKey: "pic3") {
imageListe.append(oImg3)

}
}
}
guard let url = URL(string: "http://abc.des.com/Test/SSS/feedback") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
let boundary = generateBoundary()
request.setValue("multipart/form-data; boundary=(boundary)", forHTTPHeaderField: "Content-Type")
let dataBody = createDataBody(withParameters: parameters, media:imageList , boundary: boundary)
request.httpBody = dataBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data
{
do
{
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any]
{
//some code in here
}
}
catch
{
print(error)
}
}
}.resume()
}

只要尝试一下,希望这能奏效。我做了一些改变。

型号:

struct Media {
let key: String
let filename: String
let data: Data
let mimeType: String
init?(withImage image: UIImage, forKey key: String) {
self.key = key
self.mimeType = "image/jpeg"
self.filename = "fotograf.jpg"
guard let data = image.jpegData(compressionQuality: 1) else { return nil }
self.data = data
}
}

代码:

func saveFeedBack(feedbackType: String, feedbackText: String, picture1: Data, picture2: Data, picture3: Data)
{
let parameters = [
"ofeedbackType": feedbackType,
"ofeedbackText": feedbackText
]
guard let oimage1 = UIImage(data: picture1) else { return }
guard let oimage2 = UIImage(data: picture2) else { return }
guard let oimage3 = UIImage(data: picture3) else { return }

let mediaImage1 = Media(withImage: oimage1, forKey: "pic1")
let mediaImage2 = Media(withImage: oimage2, forKey: "pic2")
let mediaImage3 = Media(withImage: oimage3, forKey: "pic3")
guard let url = URL(string: "http://abc.def.com/Mobile/SSS/feedback") else { return }
var request = URLRequest(url: url)

request.httpMethod = "POST"
let boundary = generateBoundary()
request.setValue("multipart/form-data; boundary=(boundary)", forHTTPHeaderField: "Content-Type")
let dataBody = createDataBody(withParameters: parameters, media: [mediaImage1, mediaImage2, mediaImage3], boundary: boundary)
request.httpBody = dataBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data
{
do
{
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any]
{
//some code in here
}
}
catch
{
print(error)
}
}
}.resume()
}

相关内容

  • 没有找到相关文章

最新更新