Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-22-2008, 10:51 AM   #1 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 22
Question 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.

Here is the relvent code...

Code:
@interface TestViewController : UIViewController {
	IBOutlet UIImageView *imgView;
}


@implementation TestViewController
- (void)loadView {
	
	UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"theimagefile" ofType:@"png"]];
	
	imgView = [[UIImageView alloc] initWithImage:img];
	[imgView setUserInteractionEnabled:NO];	
	
	[super loadView];
}

@end
this is being loaded like this...
Code:
[self.navigationController pushViewController:viewToLoad animated:YES];
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?

Thanks in advance...
ichi is offline   Reply With Quote
Old 10-22-2008, 11:05 AM   #2 (permalink)
Mobile Geek
 
Join Date: Aug 2008
Location: Florida, USA
Posts: 365
Send a message via AIM to rames44 Send a message via Yahoo to rames44
Default

Change
Code:
[super loadView];
to

Code:
self.view = imgView;
You need to get the view you created set into the viewController's "view" property.
rames44 is offline   Reply With Quote
Old 10-22-2008, 11:41 AM   #3 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 22
Default

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?

Last edited by ichi; 10-22-2008 at 11:46 AM.
ichi is offline   Reply With Quote
Old 10-22-2008, 12:11 PM   #4 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 22
Default

I've tried this as well... Still can't get it to load...

Code:
		[self.navigationController pushViewController:self.testView animated:YES];
		
		UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myimagefile" ofType:@"png"]];
		self.testView.imgView = [[UIImageView alloc] initWithImage:img];
ichi is offline   Reply With Quote
Old 10-22-2008, 12:22 PM   #5 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 22
Default

Got it!!!

Code:
[self.navigationController pushViewController:self.testView animated:YES];
		
		UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myimagefile" ofType:@"png"]];
	[self.testView.imgView setImage:img];
ichi is offline   Reply With Quote
Old 10-22-2008, 12:23 PM   #6 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

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.
PhoneyDeveloper is offline   Reply With Quote
Old 10-22-2008, 12:27 PM   #7 (permalink)
Mobile Geek
 
Join Date: Aug 2008
Location: Florida, USA
Posts: 365
Send a message via AIM to rames44 Send a message via Yahoo to rames44
Default

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".
rames44 is offline   Reply With Quote
Old 10-22-2008, 12:27 PM   #8 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 22
Default

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...

Code:
UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://someserver.com/someimage.jpg"]]] retain];
Thanks for explaining that though. Very helpful.
ichi is offline   Reply With Quote
Old 10-22-2008, 12:39 PM   #9 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 22
Default

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];
}
Thanks for the advice, you helped me out a lot.

Last edited by ichi; 10-22-2008 at 12:41 PM.
ichi is offline   Reply With Quote
Old 09-02-2009, 11:14 AM   #10 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 48
Default 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?
movemaker is offline   Reply With Quote
Old 01-21-2010, 03:17 AM   #11 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 1
Default

Quote:
Originally Posted by movemaker View Post
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.

Regards,

Marc Scheib | iPhone Software Developer
www.medienprodukt.com
Medienprodukt is offline   Reply With Quote
Old 02-20-2010, 04:59 PM   #12 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: Atlanta
Posts: 411
Default

Using that code 'img' is coming back as nil. So I printed the URL to console.
Code:
2010-02-20 16:36:56.281 MyApp[6759:207] URL: '
http://www.servername.com/test_thumb.jpg'
I'm getting this URL from a parsed XML field:
Code:
<picurl>http://www.servername.com/test_thumb.jpg</picurl>
So I cleaned up the URL with the following code snippet:
Code:
NSString *trimmedString = [oldstring stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
But now, my view stalls loading. How would I do a "please wait" spinner so the image loads after the view appears?
funkytaco is offline   Reply With Quote
Old 02-20-2010, 05:20 PM   #13 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: Atlanta
Posts: 411
Red face

This did the trick.
Code:
	[self performSelector:@selector(setThumbnail:) withObject:thisObject.picurl afterDelay:0.0];
funkytaco is offline   Reply With Quote
Old 12-04-2010, 12:52 PM   #14 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 4
Default thank you very much

Hi,buddy
Thank you
It works
rc20rd is offline   Reply With Quote
Old 02-27-2011, 05:43 PM   #15 (permalink)
Registered Member
 
Join Date: Oct 2010
Location: PA
Posts: 11
Default

Quote:
Originally Posted by ichi View Post
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];
}
Thanks for the advice, you helped me out a lot.

Worked perfect thanks so much!
RyanG is offline   Reply With Quote
Reply

Bookmarks

Tags
uiimage, uiimageview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 247
20 members and 227 guests
ADY, bookesp, ckgni, dacapo, Dani77, DarkAn, Davey555, Desert Diva, HemiMG, iDifferent, jakerocheleau, JasonR, LEARN2MAKE, prchn4christ, Rudy, ryantcb, Speed, themathminister, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,766
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:40 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0