Search code examples
iosswiftxcodeuiimageviewblob

How to use an image stored as blob in mysql in swift 5 UIImageView?


This is what I have.

import Foundation

struct UserData: Decodable {
    let id: Int
    let first_name: String
    let last_name: String
    let gender: String
    let email: String
    let joined: String
    let profile_image: [String:[String]]?
    let rating: Float
    let rating_count: Int
    
}

I get:

typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "profile_image", intValue: nil), _JSONKey(stringValue: "type", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a string/data instead.", underlyingError: nil))

If I change struct to:

struct UserData: Decodable {
    let id: Int
    let first_name: String
    let last_name: String
    let gender: String
    let email: String
    let joined: String
    let profile_image: [String:String]?
    let rating: Float
    let rating_count: Int
    
}

I get:

typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "profile_image", intValue: nil), _JSONKey(stringValue: "data", intValue: nil)], debugDescription: "Expected to decode String but found an array instead.", underlyingError: nil))

Not sure how to get the Image that is stored as blob in db and use it as UIImage.

This is how it comes back as response:

 \"profile_image\" =     {\n        data =         (\n            47,\n            57,\n            106,\n            47,\n            52,\n            65,\n            65,\n            81,\n            83,\n            107,\n            90,\n            74,\n            82,\n            103,\n            65,\n            66,\n            65,\n            81,\n            69,\n            65,\n            83,\n            65,\n            66,\n            73,\n            65,\n            65,\n            68,\n            47,\n            50,\n            119,\n            66,\n            68,\n            65,\n            65,\n            89,\n            69,\n            66,\n            81,\n            89,\n            70,\n            66,\n            65,\n            89,\n            71,\n            66,\n            81,\n            89,\n            72,\n            66,\n            119,\n            89,\n            73,\n            67,\n            104,\n            65,\n            75,\n            67,\n            103,\n            107,\n            74,\n            67,\n

..... it keeps going but cut off because way to long


Solution

  • struct UserData: Decodable {
        let id: Int
        let first_name: String
        let last_name: String
        let gender: String
        let email: String
        let joined: String
        let profile_image : Image
        let rating: Float
        let rating_count: Int
        
    }
    
    struct Image: Codable {
        var data: [UInt8]
    }