views:

77

answers:

3

Hi,

I'm using a piece of code I have used several times before to load an image into a UIImageView

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"1" ofType:@"png"]];
    [self.imgWeather setImage:img];
}

For some reason I get a SIGABRT when I hit [self.imgWeather setImage:img];

The image and UIImageView seems to be initialized correct.

Any idea what's going on?

[EDIT]

It seems to be crashing when I setImage to nil. Not good. I just looked at the stackTrace and can see a 'doesNotRecognizeSelector' exception:

#9  0x02554651 in objc_exception_throw ()
#10 0x0240842b in -[NSObject(NSObject) doesNotRecognizeSelector:] ()
#11 0x02378116 in ___forwarding___ ()

The imgWeather is defined as:

@property (nonatomic, retain) IBOutlet UIImageView *imgWeather;

any idea what this mean?

A: 

The first thing I would do is to make absolutely sure your UIImageView object is initialized. One easy way is simply to add some code to check it for nil and print a line to the console with the result.

Silverlock
That way `self.imgWeather` would be `nil` and because messaging `nil` is OK in Obj-C I don't see why this would be the cause of the SIGABRT.
bddckr
either UIImageView or UIImage is nil
Cipramill
No, you CAN set a UIImageView's image to nil (it means "no image") so even if the image was nil, it wouldn't be a problem.
Kalle
A: 

I much prefer this syntax:

[UIImage imageNamed:@"1.png"]

FWIW. Give that a try just for the heck of it.

ZaBlanc
That's not the same, and results in caching of the image in much different ways. Unless this is a small image that is used repeatedly throughout his application, I recommend against using this.
Kalle
+1  A: 

If self.imgWeather is nil - nothing bad would happen. I suspect self.imgWeather is not nil but it is not a UIImageView anymore and you are hitting a different object or just some random memory block(perhaps it has been released, there might be a memory problem in your app).

Does

[self.imgWeather setImage:Nil];

create a problem as well?

krzyspmac
Hi Krzyspmac, I think you are right. It seems to be crashing when I setImage to nil. Not good. I just looked at the stackTrace and can see a 'doesNotRecognizeSelector' exception: #9 0x02554651 in objc_exception_throw ()#10 0x0240842b in -[NSObject(NSObject) doesNotRecognizeSelector:] ()#11 0x02378116 in ___forwarding___ ()any idea what this mean? The imgWeather is defined as: @property (nonatomic, retain) IBOutlet UIImageView *imgWeather;
Cipramill
I had made a very stupid error, and had not used @synthesize on imgWeather. Thanks for the suggestion. Made me look in the right direction
Cipramill