Ios 博士后&x2B;完美+;Swift将UIImage上载为Base64

Ios 博士后&x2B;完美+;Swift将UIImage上载为Base64,ios,swift,postgresql,base64,perfect,Ios,Swift,Postgresql,Base64,Perfect,我要做的就是让用户挑选照片,上传到服务器上,然后解码并显示出来。我现在做的是对图像进行编码,然后将其作为Base64传递给模型。我在PosgtgreSQL中将其存储为bytea let imageb: NSData = UIImageJPEGRepresentation(image, 0.7)! as NSData let dataDecoded: String = imageb.base64EncodedString(options: .lineLength64Characters) let

我要做的就是让用户挑选照片,上传到服务器上,然后解码并显示出来。我现在做的是对图像进行编码,然后将其作为
Base64
传递给模型。我在
PosgtgreSQL
中将其存储为
bytea

let imageb: NSData = UIImageJPEGRepresentation(image, 0.7)! as NSData
let dataDecoded: String = imageb.base64EncodedString(options: .lineLength64Characters)
let imageStr: String = dataDecoded.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
然后在我的模型里面我有

private var JSON: [String: Any] {
    get {
        return ["data": self.imageData]
    }
}
然后,我使用Alamofire将其发布到服务器上

APILayer.shared.request(parameters: self.JSON, url: "photo/create", method: .post) { responce in
    //some handling stuff
}
服务器端客户端网关方法

let photos = user.getPhotos()
try! response.setBody(json: PhotoDocument.jsonFrom(array: photos))
对象方法

func getPhotos() -> [PhotoDocument] {
    return PhotoDocumentMapper.search(id: self.id)
}

private var JSON: [String: Any] {
    get {
        return ["description": self.description, "importancy": self.importancy, "date": self.date, "data": self.imageData, "id": self.id]
    }
}

class func jsonFrom(array: [PhotoDocument]) -> [String: Any] {
    var jsons: [Any] = []

    for photo in array {
        jsons.append(photo.JSON)
    }

    return ["photos" : jsons]
}
数据映射器方法

class func search(id: Int) -> [PhotoDocument] {
    let p = PGConnection()
    let _ = p.connectdb("host=localhost port=5432 dbname=perfect")
    let result = p.exec(statement: "select * from \"Photos\" where \"userId\" = $1", params: [id])
    p.close()
    var photos: [PhotoDocument] = []
    let resultsNumber = result.numTuples() - 1
    if resultsNumber != -1 {
        for index in 0...resultsNumber {
            let id = result.getFieldInt(tupleIndex: index, fieldIndex: 0)!
            let description = result.getFieldString(tupleIndex: index, fieldIndex: 4)!
            let date = result.getFieldString(tupleIndex: index, fieldIndex: 5)!
            let imageData = result.getFieldString(tupleIndex: index, fieldIndex: 3)!
            let importancy = result.getFieldInt(tupleIndex: index, fieldIndex: 2)!
            let userId = result.getFieldInt(tupleIndex: index, fieldIndex: 1)!

            let photo = PhotoDocument(userId: userId, description: description, date: date, id: id, imageData: imageData, importancy: importancy)
            photos.append(photo)
        }
    }
    return photos
}
然后我收到所有这些数据,我收到巨大的
String
,然后尝试这个,但是它在第一行崩溃了

let dataDecoded: NSData = NSData(base64Encoded: photo.imageData, options: .ignoreUnknownCharacters)! //crash here
let decodedimage = UIImage(data: dataDecoded as Data)
self.test.image = decodedimage

我做错了什么?如何将
UIImage
存储为
Base64String
作为
PostgreSQL
bytea

因此,我必须在编码时删除这一行

let imageStr: String = dataDecoded.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
并将
PostgreSQL
字段更改为
text
而不是
bytea