0

So I have an empty image view inside a .xib file. And there is another .m file that is a string but there an image loaded inside of it. Can someone please explain in detail how to load the image from the string and put it inside the imageview thats in the xib file?

jscs
  • 63,694
  • 13
  • 151
  • 195
alex
  • 27
  • 4

2 Answers2

1

If it's base64 string, you could use this type of website to extract it : http://codebeautify.org/base64-to-image-converter

In swift you could use this code to get the Image:

    let dataFromBase64 = NSData(base64EncodedString: yourString, options: NSDataBase64DecodingOptions(rawValue: 0))!
    let image = UIImage(data: dataFromBase64)!
  yourImageView.image = image

Hope it helps.

Pierre

Pierre Perrin
  • 335
  • 3
  • 13
0

For Swift 3.

extension String {
 public func base64ToImage() -> UIImage? {
        if let dataDecoded = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters) {
            return UIImage(data: dataDecoded)
        }
        return nil
    }
}

Them you call:

if let image = yourBase64Image.base64ToImage() {
   imageView.image = image
}

Hope it helps.

José Neto
  • 530
  • 2
  • 10