3

I get json response from java rest webservice it contains an image binary data and I can't convert this binary data and show the image in UIImageView

I try this in swift but it returns nil

let image = [Int]()  
let uintArray = intArray.map { UInt(bitPattern: $0) }
let dataImage = NSData(bytes: uintArray, length: uintArray.count)
FaFa
  • 59
  • 10
  • How are you sending binary data in JSON ? – 11thdimension Jul 15 '16 at 20:02
  • I get image as array of byte from json – FaFa Jul 15 '16 at 20:04
  • JSON is a string format, it doesn't support binary data. Are you converting data to base64 or something ? – 11thdimension Jul 15 '16 at 20:08
  • I can deserialize json and get this array like [-37, 100, -16, -10, -39, 31 ,etc] – FaFa Jul 15 '16 at 20:11
  • I still don't get it, but you probably can try deserializing the data on the server itself to produce the image file. That will make sure that response was produced correctly. – 11thdimension Jul 15 '16 at 20:12
  • I am sure about the response because android app can load the image but in ios I can convert this array to nsdata – FaFa Jul 15 '16 at 20:15
  • I think this is what you're looking for http://stackoverflow.com/a/11860928/5343269 – 11thdimension Jul 15 '16 at 20:58
  • thanks for the link but I need swift code – FaFa Jul 15 '16 at 22:05
  • i have same issue...have you found any solution? – H Raval Apr 13 '17 at 13:23
  • let str1 = cadenaImagen.stringByReplacingOccurrencesOfString("[", withString: "") let str2 = str1.stringByReplacingOccurrencesOfString("]", withString: "") let str = str2.stringByReplacingOccurrencesOfString(" ", withString: "") var strings = str.componentsSeparatedByString(",") var bytes = [UInt8]() for i in 0...strings.count-1 { if let signedByte = Int8(strings[i]) { bytes.append(UInt8(bitPattern: signedByte)) } } let datos: NSData = NSData(bytes: bytes, length: bytes.count) – FaFa Apr 14 '17 at 17:25
  • I fixed it with this part of code i hope it can help u :) – FaFa Apr 14 '17 at 17:27

0 Answers0