Convert HEX String to UIImage - ios

I have some request that returns a JSON string as the response.
I use the Alamofire framework, and in the function where I parse the JSON response:
if let img = jsonData["my_data"]["img"].string{
printl(img)
}
After that, I get the description
img = "\xffd8ffe000104a46494600010100000100010000ffdb0043000302020302020303030304030304050805050404050a070706080c0a0c0c0b0a0b0b0d0e12100d0e110e0b0b1016101113141515150c0f171816141812141514ffdb00430103040405040509050509140d0b0d1414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414ffc000110801e0035503012200021101031101ffc4001f0000010501010101010100000000000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b51100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a43444546474"
I need to convert this string into a UIImage. I tried doing it using the following code:
if let img = jsonData["status_order"]["img"].string {
let myData = img.dataUsingEncoding(NSUTF8StringEncoding)
// println(myData)
let base64String = myData!.base64EncodedStringWithOptions(.allZeros)
// println(base64String)
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0))
var decodedimage = UIImage(data: decodedData!)
println(decodedimage)
my_pic.image = decodeimage // I get nil
}
But after that I get some errors:
2015-09-18 18:57:14.363 taxi-admin[1520:3e03] ERROR: unable to get the
receiver data from the DB!
2015-09-18 18:57:16.822 taxi-admin[1520:4e03] ERROR:
ForceShrinkPersistentStore_NoLock -delete- We do not have a BLOB or
TEXT column type. Instead, we have 5.

Related

Convert Raw JSON response to image

I have a server response which is in json format and I have an image sent as a raw data in the response. I want to convert it into image and put it in the imageView that I have. The value for the image in json response is :
"storedisplayimage" : "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQy419TQ6bt9rpJqkugoopy9sQ3R2vzzOTjkMqK3mb
store what JSON throws you so you can displayed later while convert
override func viewDidLoad() {
super.viewDidLoad()
let userImage = UserDefaults.standard.string(forKey: "PictureFromJson")
let imageBase64 = ConduPerfilViewController.convertBase64ToImage(imageString: userImage!)
viewphoto.image = imageBase64
}
convert JSON base64 to IMG
//convert base64ToImage
class func convertBase64ToImage(imageString: String) -> UIImage {
let imageData = Data(base64Encoded: imageString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
return UIImage(data: imageData)!
}

Base64 encoding with Swift 3

How do you encode an image to base64 in Swift 3.0?
I tried to do it this way:
let imageData = UIImageJPEGRepresentation(globalImage!, 75)
let string64 = imageData!.base64EncodedString()
where globalImage is my image. I successfully sent the data to my web server but when I try to load the image it's not in a form that my computer can recognize.
Here are the encoding and decoding methods.
func encodeImageToBase64(image : UIImage) -> String{
let imageData : Data = UIImagePNGRepresentation(image)! as Data
let strBase64 = imageData.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
return strBase64
}
func decodeBase64ToImage(base64 : String) -> UIImage{
let dataDecoded : NSData = NSData(base64Encoded: base64, options: NSData.Base64DecodingOptions(rawValue: 0))!
let decodedimage : UIImage = UIImage(data: dataDecoded as Data)!
return decodedimage
}
Your second parameter is 75, it should be 0.7
let imageData = UIImageJPEGRepresentation(globalImage!, 0.7)

Decoding base64String into UIImage in Swift 2

I have a base64String that I need to convert into a UIImage() and display in a UIImageView. I've been trying for hours to convert it, but I keep getting nil from UIImage(data: data). When I print(data)it looks normal, but I can't decode it properly into an image. What's going on, and why is this code not working?
let base64String = "data:image/x-icon;base64,AAABAAEAEBAAAAAAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAQAQAAAAAAAAAAAAAAAAAAAAAAAA2Gin/Nhop/zYaKf82Gin/Nhop/zYaKf82Gin/Nhop/zYaKf82Gin/Nhop/zYaKf82Gin/Nhop/zYaKf82Gin/Nhop/zYaKf88IjD/XUlU/2RRW/9GLzv/kouQ/6Sdn/84Hiz/RCs4/0QrOP82Gyn/Nhop/0UtOv9FLDr/OR8t/zYaKf85Hyz/ubO2////////////7+/v//////+6tbb/RS47/+bl5v/Py87/OR4s/0gxPf/z8/P/vrq8/1dFTv82Gin/a1xk//////+4s7f/hnuB//b29v/+/v7/SjdC/0EoNv/5+Pn/39zg/zgdK/9oWGD//////5OIjf9ELTn/Nhop/6CYnP//////X0xW/zYaKf/Cvb///////0czPv9AJzX/9vX2/+Dc3/86Hy7/lIqP//////9jUlr/OB0s/zYaKf+qpKb//////2ZSXf82Gin/zcbJ//////9RPkf/QCc1//X19f/c2Nv/Pyc0/8nGyP/8/Pz/RzA8/zYaKf82Gin/q6Wn//////9kUVv/Nhop/83Gyf//////UD1G/0AnNf/19fX/1NDT/0w4RP/5+fn/zcnL/zkfLf82Gin/Nhop/6ulp///////ZFFb/zYaKf/Nxsn//////1A9Rv9AJzX/8fDx//n4+f/U0NP//////6GYnP82Gin/Nhop/zYaKf+rpaf//////2RRW/82Gin/zcbJ//////9QPUb/QCc0//Dv7///////+vr6///////39vf/RzM//zYbKf82Gin/q6Wn//////9kUVv/Nhop/83Gyf//////UD1G/0AnNP/19PT/4N3e/0s0QP+HfIL//////5uRlv9HLzv/Nhop/6ylp///////ZFFb/zYaKf/Nxsn//////1A9Rv9AJzT/9fX1/9zZ2v85Hiz/VD9K//////+7tLf/VkJM/zYaKf+po6X//////2RRW/82Gin/xb3C//////9OPEX/QCc0//b19f/c2tr/OR4s/1I9SP//////t7Gz/1M/SP82Gin/joWJ//////98b3b/Ri47/9XS1P//////STI+/0MpNv/y8fH/6+np/3FfaP+poab//////46Dif9CKjf/Nhop/0c0P//39vf///////r6+///////pp+k/zYaKf9GLjr/8O/v//////////////////Dv7/9AKjb/Nhop/zYaKf82Gin/UkBJ/7Osrv/DvsD/joSJ/zkhLv82Gin/PCIw/35wdv+GeH7/hHd8/39yef9HMT3/Nhop/zYaKf82Gin/Nhop/zYaKf82Gin/Nhop/zYaKf82Gin/Nhop/zYaKf82Gin/Nhop/zYaKf82Gin/Nhop/zYaKf82Gin/AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//w="
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
print(decodedData) // prints out fine
let image = UIImage(data: decodedData!) // keep getting nil here
use this function for decode Base64 String to String
public func decodebase64(str : String) -> String{
let decodedData = NSData(base64EncodedString: str, options:NSDataBase64DecodingOptions(rawValue: 0))
let decodedString = String(data: decodedData!, encoding: NSUTF8StringEncoding)
return decodedString!}
then use this function in this way
let newimage:String? = decodebase64(busimage)
if (newimage?.characters.count != 0)
{
ImageLoader.sharedLoader.imageForUrl(newimage!) { (images, url) -> () in
if (images != nil)
{
cell.compnylogo.image = images!
}
}
}
here you have to just use. or drag and drop this swift file.
Use this image Loader File this will help you

NSDataBase64DecodingOptions always returns nil

There are a lot of similar questions but non of them helped me out so I just don't know what to do but ask. So this is how I encode a UIImage:
let data: NSData = UIImagePNGRepresentation(imageResized)!
let base64String: NSString = data.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
and decoding (as suggested here):
if let range = base64.rangeOfString("data:image/png;base64,", options: .AnchoredSearch) {
base64.removeRange(range)
}
let decodedData = NSData(base64EncodedString: base64, options: NSDataBase64DecodingOptions(rawValue: 0))
if let decodedImage = UIImage(data: decodedData!) {
self.imageList.append(decodedImage)
}
But the app crashes when initializing decodedData and I can't figure out why.
I checked my base64 string here and it returns the picture.
Any help will be very appreciated!
Try with this:
if let decodedData = NSData(base64EncodedString: base64, options:NSDataBase64DecodingOptions.IgnoreUnknownCharacters){
if(decodedData.length > 0){
let imageDecodeData = UIImage(data: decodedData)!
}
else{
print("error")
}
}
Replace the line with let decodedData with:
let decodedData = NSData(base64EncodedString: base64, options: [])
And also make sure that your using correct variables:
// 1. This should be a variable if you're mutating it.
let base64String = data...
// 2. You should probably do this on your base64String
if let range = base64.rangeOfString(...)

Convert String from JSON to UIImage

I'm receiving some JSON data from a webservice, which includes an image represented as a Sting.
How can I convert this string into an image?
The JSON data is in an NSDictionary, and the Image data is the Object form the "Content"-key:
if let newBannerContentString = newBanner.objectForKey("Content") as? String {
let someImage = UIImage(contentsOfFile: newBannerContentString)
}
This returns nil to someImage.
If string is base64 encoded you could create NSData from that string and image from that data.
if let newBannerContentString = newBanner.objectForKey("Content") as? String {
let data = NSData(base64EncodedString: newBannerContentString, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters);
let someImage = UIImage(data: data!);
}

Resources