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 06-13-2008, 05:06 PM   #1 (permalink)
Registered Member
 
Join Date: May 2008
Posts: 13
Default display image from xml

HI,
I am parsing an xml and getting url of image from that xml.
Now I want to display that image from url.
This is my code

- (void)setHomeHome *)newHome
{
[newHome retain];
[_home release];
_home = newHome;

self.bedsLabel.text = newHome.details ;
self.cszLabel.text = newHome.csz;
self.addressLabel.text = newHome.address;
self.price.text = newHome.price;
self.propImageView.image = [self imageFromURL:newHome.picture];
[self setNeedsDisplay];
}

- (UIImage *)imageFromURLNSString *)url {
//NSString *temp = @"http://imgprd.nrtwebservices.com/California/properties/JPG_Main_White/222/7321C37C-80B4-43FD-B4E3-A01AF52B7222_9.jpg";
//The above code which is commented works fine for me when I give direct Image URL
NSString *temp = url;//But when I try this this doesnt work.

NSURL *url1 = [NSURL URLWithString:temp];
NSData *data = [NSData dataWithContentsOfURL:url1];
UIImage *propImage = [[UIImage alloc] initWithData:data cache:NO];
return propImage;
}


Any Idea what is going on?
If anybody can help me plz.
Thanks.
sangau001 is offline   Reply With Quote
Old 06-14-2008, 02:02 PM   #2 (permalink)
New Member
 
Join Date: Apr 2008
Location: Onomatopoeia, Lugubriousylvania
Posts: 225
Default Re: display image from xml

What is the error you're getting?

Have you checked to see whether newHome.picture is a complete URL?
bonehead is offline   Reply With Quote
Old 06-15-2008, 03:09 PM   #3 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 420
Send a message via AIM to jeff_lamarche Send a message via Yahoo to jeff_lamarche
Default Re: display image from xml

Did you try using NSlog to print out the value of the url, or did you look at its value in the debugger? Maybe it got messed up in the parsing, maybe there's a special character that needs to be url-escaped, etc. It could be any number of things - you'll need to provide more info before anybody can help you here, I'm afraid. Also, are you checking url1 to make sure that it's creating a valid URL?

And a few general comments about your code:

Why are you assigning url to a stack local variable? I don't see any reason for doing that. It makes your code harder to read and uses unnecessary space on the stack. Also, you're leaking here, you allocated memory for propsImage, but you never release it. You are also using convenience methods, which is generally a no-no on the iPhone because they require using the autorelease pool unnecessarily.

Lastly, although this is picayune, I would use more descriptive variable names. Don't call an NSString variable "url", that's potentially confusing a few months from now when you're debugging this code and haven't looked at it in months.

Here's a stab at a quick rewrite of imageFromURL:, although, I'm actually not convinced that this should be in a method at all. By putting this functionality in a method rather than doing it inline, you're forced to put at least one thing in the autorelease pool, which is something you want to avoid on the iPhone because it will not use the swap file for volatile memory, meaning you are very much memory constrained and it doesn't take a whole lot of leaked image files to use up the available memory - on current generations of the iPhone, your application can expect best case scenario of 60-80 megs of RAM for its use. Call your original method a few dozen times and you're in a low-memory situations and your app will crash. (Remember, Instruments is your friend - it has a template to find leaks like this).

Code:
- (UIImage *)imageFromURL:(NSString *)urlString {
	NSURL *url = [[NSURL alloc] initWithString:urlString];
	NSData *data = [[NSData  alloc] initWithContentsOfURL:url];
	[url release];
	UIImage *propImage = [[UIImage alloc] initWithData:data];
	[data release];
	return [propImage autorelease];
}
So, that's better then what you have, but I think you're even better off doing it like this inline in your setHome method like this (remember, I can't compile this code, so there might be some minor issues with it):

Code:
- (void)setHome:(Home *)newHome
{
	[newHome retain];
	[_home release];
	_home = newHome;
	
	self.bedsLabel.text = newHome.details ;
	self.cszLabel.text = newHome.csz;
	self.addressLabel.text = newHome.address;
	self.price.text = newHome.price;
	NSData *data = [[NSData  alloc] initWithContentsOfURL:self.picture];
	[url release];
	UIImage *propImage = [[UIImage alloc] initWithData:data];
	[data release];
	self.propImageView.image = propImage;
	[propImage release];
	[self setNeedsDisplay];
}
One last thing - I have two comments about this line of code:

Code:
 [self setNeedsDisplay]
First, setNeedsDisplay is VERY costly on the iPhone. Unless this change affects the vast majority of the view (and it might), you should use setNeedsDisplayInRect: instead of just setNeedsDisplay. The display algorithms on the iPhone are optimized for partial redraws.

Secondly, this looks like classic model or controller code (it's a grey area, I could see arguments for both) - but it looks like you're doing it inside the mutator of a UIView subclass, meaning you're doing it in the one place you probably shouldn't be according to the tenants of MVC - in the View component, or else you don't have a clear distinction between model and view components which will make it harder to maintain your code down the line.

Now I could be missing something here, since since I'm working from two methods completely out of context), but your code seems to violate MVC, so you might want to take a step back and just make sure you're happy with the overall design of your code.

I hope this helps. I know it's not specifically the problem you're looking to get fixed, but some of these things - especially the memory leaks - will bite you down the line. Let us know what you find in the debugger, and we can maybe get your original problem resolved too.
__________________
Check out my iPhone Dev Blog
You can send me e-mail at my forum username at mac dot com.
jeff_lamarche is offline   Reply With Quote
Reply

Bookmarks

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tinting an image? moopf iPhone SDK Development 2 01-28-2009 04:56 AM
How to display image from URL in UITableView odbc2000 iPhone SDK Development 1 05-22-2008 06:40 AM
UIButton with an image ddavtian iPhone SDK Development 1 04-30-2008 03:53 PM
Modal display is not working correctly Fastrak iPhone SDK Development 9 04-21-2008 07:54 PM
Help with Image View Nitrex88 iPhone SDK Development 4 04-19-2008 01:02 AM


» Advertisements
» Online Users: 259
23 members and 236 guests
@sandris, ADY, Dani77, diyora, FAED, fredidf, F_Bryant, GHuebner, HDshot, iDifferent, JasonR, mer10, Oral B, prchn4christ, Rudy, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,753
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

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