Applying Base64 String to UIImageView not working in iOS Swift 2

I have a Base64 string saved from Swift into mysql db. When it comes back, it does not display an image.

How it looks in the db:


and this is how I am attempting to receive it but its not working:

let partUrl = "data:image/jpg;base64,";
let appndd = partUrl.stringByAppendingString(baseStringNew!)

let urlWeb = appndd
let requestURL: NSURL = NSURL(string: urlWeb)!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()

let task = session.dataTaskWithRequest(urlRequest) {
   (data, response, error) -> Void in

   if error == nil {

      dispatch_async(dispatch_get_main_queue(), {

      imageNewView.layer.cornerRadius = 10.0
      imageNewView.clipsToBounds = true

      // Adding a border to the image profile
      imageNewView.layer.borderWidth = 1.0
      imageNewView.layer.borderColor = UIColor.grayColor().CGColor
      imageNewView.image = UIImage(data:data!)


  } else {

 } //end of task


This displays a blank image in the UIImageView. How do I get this string to show up as an image?


Make sure that you prepend your data with data:image/png;base64.

Here is an q&a about this, How to display a base64 image within a UIImageView?

You might want to check the base64 string you stored and retrieved from your MySQL DB. Try using BLOB data type.

Then converting base64 string to NSData then UIImage is pretty easy and straightforward.

let rawData = NSData.init(contentsOfURL: NSURL(string: "")!)!
let base64String = rawData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
let imageData = NSData(base64EncodedString: base64String, options: .IgnoreUnknownCharacters)
let image = UIImage(data: imageData!)

Need Your Help

Setting a toolbar background color based on theme attribute crash the app

android colors reference attributes toolbar

So, i am setting a color of the toolbar by choosing the theme from settings...the color should be the color_primary.

Custom totals collector: Reorder totals in totals overview in checkout

magento totals

I have created a custom totals collector to grant qualified customers a discount of 3% of the cart's subtotal. The code of my collector looks like the following: