0

How can I response byte array from the server and convert it to image. This my code enter image description here

Alamofire.request(mainUrl, method:.post , parameters: paramstring , encoding: JSONEncoding.default, headers: nil).responseJSON { response in
        if let data = response.data
        {
            switch response.result
            {
            case.failure(let error):
            print(error)
            case.success(let value):
                let json = JSON(value)
                guard let dataarr = json["my_profile"].arrayObject as? [String] else {return}
                let image = dataarr[0]
                let mydata = image.data(using: String.Encoding.utf8)! as NSData
                print(mydata)
                let mybase64 = mydata.base64EncodedData(options: NSData.Base64EncodingOptions.endLineWithLineFeed)
                print(mybase64)
                self.MainView.avatarImageView.image = UIImage(data: mybase64)

            }
        }

as you can see , its not byte array , and it continue to 1000 rows enter image description here

  • Could you paste the json data that you recieve? – Vinaykrishnan Jun 13 '18 at 11:52
  • This is a small part of json , as you can see it's not byte array "my_profile": [ "\\xffd8ffe000104a46494600010101006000600000ffdb00430001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101ffdb00430101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101ffc000110803c0050003012200021101031101ffc4001f0000010501010101010100000000000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191 – –  Jun 13 '18 at 12:04
  • Could you try this code since its a partial data https://stackoverflow.com/a/38313261/2323806 – Vinaykrishnan Jun 13 '18 at 12:06
  • I tried , but it couldn't worked –  Jun 13 '18 at 12:08
  • here I have one problem , data which on the server not same with data I response , I try to convert responses string to byte array, but I got same result , so strange :/ –  Jun 13 '18 at 12:15
  • If could paste your server response or attach a screenshot with your question, that would help – Vinaykrishnan Jun 13 '18 at 12:16
  • add your server response – a.masri Jun 13 '18 at 12:18
  • added to description –  Jun 13 '18 at 12:21

2 Answers2

1

Your data is not Base64-encoded. It's hex encoded. You need to hex-decode it, not use base64EncodedData.

First, you need to drop the first two characters (\x):

let hex = dataarr[0].dropFirst(2)

Then you need a method to convert hex to Data. There are many ways. Here's one:

extension Data {
    init?<S: StringProtocol>(hexString: S) {
        guard hexString.count % 2 == 0 else { return nil }  // Must be even number of letters

        var bytes: [UInt8] = []

        var index = hexString.startIndex
        while index != hexString.endIndex {
            let secondIndex = hexString.index(after: index)
            let hexValue = hexString[index...secondIndex]
            guard let byte = UInt8(hexValue, radix: 16) else { return nil } // Unexpected character
            bytes.append(byte)
            index = hexString.index(after: secondIndex)
        }
        self.init(bytes)
    }
}

With that, decode it:

if let data = Data(hexString: hex),
   let image = UIImage(data: data) {
       // ... use image
}
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • YOOOO MAN YOU ARE MY HERO , THANK YOU VERY MUCH , I HARD WORKED WITH THIS PROBLEM 3 DAYS –  Jun 14 '18 at 03:39
0

Replace your code

let mydata = image.data(using: String.Encoding.utf8)! as NSData
print(mydata)
let mybase64 = mydata.base64EncodedData(options: NSData.Base64EncodingOptions.endLineWithLineFeed)
print(mybase64)
self.MainView.avatarImageView.image = UIImage(data: mybase64)

With

if let mydata : Data = Data(base64Encoded: image, options: .ignoreUnknownCharacters) {
   DispatchQueue.main.async {
       self.MainView.avatarImageView.image = UIImage(data: mydata)
    }

}
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98