I am currently converting a user-picked UIImage to a base64 string using the below method, but when I go to convert that back into an image using the below method, I can get the NSData, but I can't convert that into a UIImage. Any thoughts? I have already tried using both PNG and JPEG methods for the UIImage, and neither worked if that adds any detail.
Thanks,
Jacob
Encoding Code:
var base64 = UIImagePNGRepresentation(self.profileView.image!))!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
Decoding Code:
let decodedData = NSData(base64EncodedString: String(user?.photoURL), options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
let decodedimage = UIImage(data: decodedData!) //this decoded image is nil
You can try with code:
let image : UIImage = UIImage(named:"your_image")!
let imageData:NSData = UIImagePNGRepresentation(image)!
Encoding:
let base64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
Decoding
let dataDecoded:NSData = NSData(base64EncodedString: base64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
Here is the code to convert NSData to UIImage
let image: UIImage = UIImage(data:dataDecoded,scale:1.0)
I hope this helps.
Related
//To encode I found the following example. I don't know if it works yet
#IBAction func exitImageViewController(seque:UIStoryboardSegue){
let imageController = seque.destination as! MyImageViewController
let myView = imageController.imageView
imageData = UIImagePNGRepresentation((myView?.image!)!)
imageString = imageData?.base64EncodedString()
// Is this swift code correct?
//I cannot find an example of decoding String back to UIImage
The code you posted looks fine. To encode back you can do this:
let data = Data(base64Encoded: imageString!)
let img = UIImage(data: data!)
I'm trying to convert a UIImage to a base64 string with the goal of uploading it to a back-end server.
However, the conversion code I found in this article (which should be Apple's own implementation) generates an invalid string:
Convert between UIImage and Base64 string
After upload, I get this image:
[Failty image that is decoded from iOS converted base64 1
Instead of this:
[Correct image decoded from an online base64 conversion tool2
I tested the upload results using Postman and the back-end handles a valid base64 image correctly, so I narrowed the bug down to the base64 conversion itself. Here's my code:
public extension UIImage
{
func base64Encode() -> String?
{
guard let imageData = UIImagePNGRepresentation(self) else
{
return nil
}
let base64String = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
let fullBase64String = "data:image/png;base64,\(base64String))"
return fullBase64String
}
}
Any idea how I could fix my base64 output on my iOS device before I upload it to the server?
Do it something like this:
For Encoding:
data.base64EncodedStringWithOptions([])
For decoding:
let url = URL(string: String(format:"data:application/octet-stream;base64,%#",base64String))
do {
let data = try Data(contentsOf: url!)
}catch {
}
for Swift 4,
Do something like this,
For Encoding -
let imgObj = UIImage(named: "photo")
let imageData = UIImagePNGRepresentation(imgObj!)! as NSData
let base64 = imageData.base64EncodedData(options: .lineLength64Characters)
datatype of base64 variable is Data.
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)
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
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!);
}