0

Alright so I've made some handlers and classes to grab data from a URL, its all returning fine, I've checked the URLs are valid and everything.

Anyways, I'm trying to do an NSData(contentsOfURL) on a stored URL in my class for a UIViewController. I'm successfully printing out String Variables like name, type, description, but I'm having difficulty displaying an UIImage into a Image View on the ViewController.

Here is my Code, it's run when the View loads:

func configureView() {
        // Update the user interface for the detail item.
        if let card = detailCard {
            title = card.cardTitle

            //Assign Elements
            detailDescriptionLabel?.text = card.description
            typeLabel?.text = card.cardType
            cardTitleLabel?.text = card.cardTitle
            costLabel?.text = card.cardCost

            print(card.cardImageURL)

            if let url = NSURL(string: card.cardImageURL){
                let data = NSData(contentsOfURL: url)
                imageView.image = UIImage(data: data!)// <--- ERROR HERE: EXC_BAD_INSTRUCTION
            }

            //Print Log
            //print(card.logDescription)
        }
    }

Like the comment says above, I get the error on the imageView.image = UIImage(data: data!) line:

Thread 1: EXC_BAD_INSTRUCTION

Here is the code for cardImageURL:

var cardImageURL: String {
    return String(format: "http://testyard.example.net/display/images/cards/main/%@", image)
}
//This is a computed variable when a "Card" class object is created.

It returns the correct url in a string:

http://testyard.example.net/display/images/cards/main/armor-of-testing.jpg

I've used this code elsewhere, and it worked fine, why is it throwing errors here?

Thanks in advance.

3
  • you need to show how did you declare card.cardImageURL Commented Oct 26, 2015 at 22:41
  • please show the result of print(card.cardImageURL) Commented Oct 27, 2015 at 1:24
  • Please view my edits, thank you. Commented Oct 27, 2015 at 14:23

1 Answer 1

2

Are you sure the data being downloaded is an image? You should probably use this line:

if let image = UIImage(data: data)`

This should stop the error, though you might need to test that your data actually contains an image.

Sign up to request clarification or add additional context in comments.

5 Comments

so I changed my code to if let url = NSURL(string: card.cardImageURL){ let data = NSData(contentsOfURL: url) if let image = UIImage(data: data!){ imageView.image = image } } and its stepping into the "if let image" code and still returning the error. Any ideas?
Out of interest, if you use let cgImage = CGImageCreateWithJPEGDataProvider(CGDataProviderCreateWithURL(url), nil, false, CGColorRenderingIntent.RenderingIntentDefault) and then add this instead of the if let image: dispatch_async(dispatch_get_main_queue()) {imageView.image = UIImage(CGImage: cgImage!)} - does that work?
figured it out. adding a "?" after imageView did the trick. So: imageView?.image = image
Does the image actually display now? I thought you would have only needed to add the ? if imageView could be nil.
Yeah it totally works. Although im not 100% confident in why :/ Possibly because the URL itself it wrapped in an If statement?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.