Can't get UIImageView to load an UIImage at runtime.
Hi,
Sorry for the noob question. I've been stuck on this for ages and have been searching for a way to get it to work. Time to ask for help...
I have an UIImageView on a in a ViewController and am trying to get it to load an image at viewLoad but it just will not show anything. I have no warnings or errors and am totally stuck.
I have conencted the UIImageView to the IBOutlet in Interface Builder, but it just won't load the image when I run it... Can anyone suggest anything I should try or point out where I am going wrong please?
Ahh OK. Thanks.
That did load the image but into my entire UIViewContoller, filling it. In IB I have put an UIImageView inside a UIViewController that has it's class set to TestViewController as I want other stuff in the view as well... If you see what I mean?
When the nib is loaded all of the views inside it, plus any other objects inside it, are created and initialized. You don't need to build a UIImageView in code.
In fact you don't have to load the image in code either. You can set the image for the imageView in IB. In the ImageView attributes pane in IB there's a popup where all the images in your project are listed. Just pick the one you want there. That's it.
Yeah, I got confused and thought you were doing this in straight code instead of using IB, since you don't normally implement "loadView" in IB-based apps - you normally put anything you need to do to tweak the views after they're loaded out of the nib in "viewDidLoad".
OK, I understand, it makes sense to me now. The reason I am loading the image in code is because now I have it working I am going to download the image from the web using NSData, like this...
In case this helps anyone else, I moved the image loading code out of my RootViewController where the view is loaded and into the viewDidLoad method of the class that represents the viewcontroller. Here is the final code that works...
Code:
- (void)viewDidLoad {
//taken from http://idevkit.com/forums/tutorials-code-samples-sdk/3-one-line-uiimage-url.html
UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://someserver.com/somefile.jpg"]]] retain];
if (img != nil) { // Image was loaded successfully.
[imgView setImage:img];
[imgView setUserInteractionEnabled:NO];
[img release]; // Release the image now that we have a UIImageView that contains it.
}
[super viewDidLoad];
}
Hey I tried loading the pic from IBand it didn't work
I loaded my picture by setting the imageview in interface builder to the picture I wanted. Everything shows up perfect in the simulator but when I run it on the phone the pictures don't show up why?
I loaded my picture by setting the imageview in interface builder to the picture I wanted. Everything shows up perfect in the simulator but when I run it on the phone the pictures don't show up why?
Check the Filename of the referenced Image: Filenames on the iPhone are case-sensitive, the Emulator on OSX does not care about invalid case.
In case this helps anyone else, I moved the image loading code out of my RootViewController where the view is loaded and into the viewDidLoad method of the class that represents the viewcontroller. Here is the final code that works...
Code:
- (void)viewDidLoad {
//taken from http://idevkit.com/forums/tutorials-code-samples-sdk/3-one-line-uiimage-url.html
UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://someserver.com/somefile.jpg"]]] retain];
if (img != nil) { // Image was loaded successfully.
[imgView setImage:img];
[imgView setUserInteractionEnabled:NO];
[img release]; // Release the image now that we have a UIImageView that contains it.
}
[super viewDidLoad];
}