Postgres + Perfect + Swift uploading UIImage as Base64

210 Views Asked by At

What I have to is just let user pick photo, upload it on server and then decode back and display it. What I am doing now is encode image and then pass it to a model as Base64. I store it as bytea in PosgtgreSQL.

let imageb: NSData = UIImageJPEGRepresentation(image, 0.7)! as NSData
let dataDecoded: String = imageb.base64EncodedString(options: .lineLength64Characters)
let imageStr: String = dataDecoded.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!

then inside my model I have

private var JSON: [String: Any] {
    get {
        return ["data": self.imageData]
    }
}

then I post it on server using Alamofire

APILayer.shared.request(parameters: self.JSON, url: "photo/create", method: .post) { responce in
    //some handling stuff
}

server side client gateway method

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

object methods

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]
}

datamapper method

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
}

then I receive all this data, and I receive huge String and try this, but it crashes on the first line

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

What am I doing wrong? How can I store UIImage as Base64String as bytea of PostgreSQL?

1

There are 1 best solutions below

0
On BEST ANSWER

So what I had to do is remove this line while encoding

let imageStr: String = dataDecoded.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!

and change PostgreSQL field to be text and not bytea