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?
Asked
Active
Viewed 193 times
0
-
What do you mean about String with an image loaded inside ? Is that a Base-64 string representation of the image ? Or an other representation ? – Pierre Perrin Jun 13 '17 at 18:41
-
Yes it is a Base-64 string representation – alex Jun 13 '17 at 18:48
2 Answers
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