I want to set an image as my background at runtime, so I created a UIImageView ‘backGroundImage’ as IBOutlet in Interface Builder. However the following code:
UIImage *image = [UIImage imageNamed:imageName];
NSLog(@"image=nil %d",(image ==nil));
NSLog(imageName);
[self.backGroundImage setImage:image];
NSLog(@"backgroundImage=nil %d",(self.backGroundImage.image ==nil));
gives me the following NSLogs
2012-06-15 12:09:43.967 iElster[1960:207] image=nil 0
2012-06-15 12:09:43.967 iElster[1960:207] bedroom-sketch-corel-eps-vector-art-1141.jpg
2012-06-15 12:09:43.968 iElster[1960:207] backgroundImage=nil 1
so basically I can load my image, but I can not set it as the image of the UIImageView. If I include the following line
backGroundImage = [[UIImageView alloc]init];
I don’t get ‘nil’ anymore for my backGroundImage.image – but in that case I obviously don’t use the UIImageView created by the Outlet anymore. I do not see the problem with my code above, especially since all I saw as suggestions in similar Stack Overflow threads was exactly the same: “simply use the setImage method”; Any ideas what could be the problem?
PS: just to clarify that – I tried using different images, both .png and .jpg and they all work if I e.g. just set them as a subview to my ScrollView, but not when trying to use them in the code above. So the image files shouldn’t pose any problems.
It seems that you placed this code in a method that is executed before the view gets loaded and the outlets are connected (e.g., in
init...). Try placing it inviewDidLoad. You cannot access your view outlets before the view gets loaded (i.e., beforeviewDidLoadis accessed).Also, this line:
is a memory management error (overrelease).