gpt4 book ai didi

ios - Swift:JSON 写入中的无效类型(UIImage)

转载 作者:可可西里 更新时间:2023-11-01 02:15:15 28 4
gpt4 key购买 nike

我正在尝试将 UIImage 转换为 base 64 字符串,以便可以将其保存到文件中然后上传。我正在做以下事情:

代码片段:

var writePath: String!
var params: NSMutableDictionary = ["file1": UIImageJPEGRepresentation(self.imageView1.image!, 1.0)!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)), "userId" : user_id]

do {
let bodyJson = try NSJSONSerialization.dataWithJSONObject(params, options: .PrettyPrinted)
let path: NSURL = NSURL(string: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])!
writePath = path.URLByAppendingPathComponent("body.data").absoluteString
try bodyJson.writeToFile(writePath, options: .AtomicWrite)
writePath = String(format: "file://%@", writePath)
} catch {
print(error)
}

但是,我收到无效类型错误。我必须使用后台 session ,还需要发送一些参数,所以,我正在使用这种方法。

enter image description here

已解决:其他原因导致了这个问题。

最佳答案

只是尝试解决,试试这个:

    var writePath: String!
let params: [String: String] = ["file1": UIImageJPEGRepresentation(self.imageView1.image!, 1.0)!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength), "userId" : user_id]

do {
let bodyJson = try NSJSONSerialization.dataWithJSONObject(params, options: .PrettyPrinted)
let path: NSURL = NSURL(string: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])!
writePath = path.URLByAppendingPathComponent("body.data").absoluteString
try bodyJson.writeToFile(writePath, options: .AtomicWrite)
writePath = String(format: "file://%@", writePath)
} catch {
print(error)
}

关于ios - Swift:JSON 写入中的无效类型(UIImage),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39270287/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com