0

Looking through the forums I have found that this issue is one that shows its head every now and then. And apparently doing so in a wide scope of different cases. None that I have managed to find seem to be similar to mine though.

I'm halfway through my program (lesson) in creating a usable twitter application. Testing it currently runs the program as it should, without any errors. But when I select an account the program crashes and an error message shows up at the image method which is supposed to load the avatar of the selected user. I assume that it is not able to retrieve a valid image or fetch data at all (because of the line ImageData = (NSDATA?) nil in the debug area), but I am by no means sure of anything, let alone how to or where to find a solution. If I am searching with the wrong keywords then please let me know. (I am searching for exc_bad_instruction and uiimage error) Thanks in advance.

I'll post the snippet of code where the issue presents itself below and what is shown in the debug area below that.

    if let cachedImage = image {
        cell.tweetUserAvatar.image = cachedImage
    }
    else {
        cell.tweetUserAvatar.image = UIImage(named: "camera.png")

        queue?.addOperationWithBlock() {
            let imageURL = NSURL(string: imageURLString) as NSURL!
            let imageData = NSData(contentsOfURL: imageURL) as NSData?
            let image = UIImage(data: imageData!) as UIImage? // EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subode=0x0)

            if let downloadedImage = image {
                NSOperationQueue.mainQueue().addOperationWithBlock(){
                    let cell = tableView.cellForRowAtIndexPath(indexPath) as! TweetCell

                        cell.tweetUserAvatar.image = downloadedImage
                }

                self.imageCache?.setObject(downloadedImage, forKey: imageURLString)
            }
        }
    }

Debug area:

    imageURLString  String           
    "http://pbs.twimg.com/profile_images/465756113825067008/8jH2nZO0_normal.png"    
    tableView   UITableView 0x00007fc52481b400
    indexPath   NSIndexPath *   0xc000000000000016
    self Chapter7_8___Social_App.FeedViewController 0x00007fc5235f5ef0
    imageURL    NSURL! "http://pbs.twimg.com/profile_images/465756113825067008/8jH2nZO0_normal.png" 
    imageData   NSData? nil None
    image   UIImage?    0x000000010ee778dd
    downloadedImage UIImage     
L. Klotz
  • 164
  • 1
  • 15

1 Answers1

0

I had this issue as well and found that my image was being initialized with bad data. This is to say that the image I requested from the server did not exist and the server sent back a response which could not be converted into an UIImage.

To mitigate this you can do the following:

if let cachedImage = image {
    cell.tweetUserAvatar.image = cachedImage
}
else {
    cell.tweetUserAvatar.image = UIImage(named: "camera.png")

    queue?.addOperationWithBlock() {
        let imageURL = NSURL(string: imageURLString) as NSURL!
        let imageData = NSData(contentsOfURL: imageURL) as NSData?

        if let image = UIImage(data: imageData!) {
            let downloadedImage = image
            NSOperationQueue.mainQueue().addOperationWithBlock(){
                let cell = tableView.cellForRowAtIndexPath(indexPath) as! TweetCell

                    cell.tweetUserAvatar.image = downloadedImage
            }

            self.imageCache?.setObject(downloadedImage, forKey: imageURLString)
        }
    }
}

What I have done above is changed

        let image = UIImage(data: imageData!) as UIImage?

        if let downloadedImage = image {
           ...
           ...
        }

To:

        if let image = UIImage(data: imageData!) {
           let downloadedImage = image
           ...
           ...
        }    

In this way I have checked that the image was able to be created successfully. If the image is not able to be created then the code will not execute and you will not receive an error.

If you expect an image to be at the url you specified in 'imageURLString' then I would suggest that you check the url you are using.

I also noticed that you did not get any data back which is why you could not create the UIIMage. You can test to see if this is the case with the following:

let imageData = NSData(contentsOfURL: imageURL) as NSData?
if imageData != nil {
  Do More Stuff
  ...
{

I hope my answer was helpful. If you have any questions feel free to leave a comment and I'll do my best to answer them.

More Info Here:
This question & answer also provides methods on how to handle the case when the UIImage cannot be created from the data provide.

Community
  • 1
  • 1