开发者

crash when setting image in UIImageView

开发者 https://www.devze.com 2023-01-06 19:45 出处:网络
I\'m using a piece of code I have used several times before to load an image into a UIImageView - (void)viewDidLoad {

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 g开发者_开发问答et 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?


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?


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

Your imgWeather is an outlet. Make sure it is properly linked in the IB.


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.


I much prefer this syntax:

[UIImage imageNamed:@"1.png"]

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号