2

I have a Json feed from my MySQL database and this is working great for name and other details but I also have a blob column for images that I want to connect to a UIImage in my storyboard but am unsure how to take the blob data in the Json feed and decode back into UIImage. I think the data is a seen as an String at the moment.

I don't want to use any library's so would a pointer to a server image location (aka a URL) in the database be better option than blob ? I guess so ? If so how do I display that from Json feed?

Thanks

2 Answers 2

2

There are a lot of questions here.

  1. How did the image get inserted into the database?
  2. Was it encoded in some way?
  3. How was it retrieved?

You don't specify the answer here, so I am assuming it's raw image bytes. If that's not true, add some encoding or decoding as needed.

You can convert the string to NSData easily enough, then create a UIImage from it:

var imagedatastr:String = "" // assume this is the data you got from MySQL

if let imageData:NSData = imagedatastr.dataUsingEncoding(NSUTF8StringEncoding)
{
    let image = UIImage(data:imageData,scale:1.0)
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes sorry I posted the image in via Swift app / PHP the json feed comes back with image = "Optional(<UIImage: 0x6100002881b0> size {4288, 2848} orientation 0 scale 1.000000)";
0

Normally you have a string, and then you will show the image into a uiimageview, ok. Finally, you consider using a thread like this.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))
    {
        let data = NSData(contentsOfURL: NSURL(string: <yourUrlString>)
        var image: UIImage?
        if data != nil {
            artist.vImageData = data
            image = UIImage(data: data!)
        } else {
            image = UIImage(named: <defaultImageLikeNoAvailable>)
        }

        dispatch_async(dispatch_get_main_queue()) {
            imageView.image = image
        }
    }

2 Comments

I cannot get either example to show the image in my uiimageview - the error on example one is that I cannot put it into uiimageview should be UIImage but that does t work and I have no idea what the dispatch one is doing sorry?
Still struggling to get image to show in UIImageView any additional pointers please ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.