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 05-27-2009, 10:13 AM   #1 (permalink)
New Member
 
Join Date: May 2009
Posts: 2
Exclamation Display images from XML!! HELP!!

Hey everyone,
I'm trying to display an image from an xml file online. The xml file has a <img>myurl</img> structure and the xml is parsed and than fed to an NSDictionary and than to a NSArray.

Here's the code. Now I know that i'm basically asking it to fetch a string of text, and not an image, but i've looked all over the web trying to find something but without sucess.

Code:
- (id)initWithDictionary:(NSDictionary *)dict {
	self = [super init];
	
	if (self) {
		self.title = @"Details";
		self.detailDictionary = dict;
		self.categories = [[dict valueForKey:@"img"] componentsSeparatedByString:@", "];
				
		[[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(backButton:)] autorelease]];
		
		categoryPosition = 0;
	}

Thanks for any help and insight!!!
Lordpanzer is offline   Reply With Quote
Old 05-28-2009, 09:37 AM   #2 (permalink)
New Member
 
Join Date: May 2009
Posts: 2
Default

Nobody????? pleaseeeee!!
Lordpanzer is offline   Reply With Quote
Old 05-28-2009, 10:39 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,556
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

are you using NSXMLParser anywhere?
smithdale87 is offline   Reply With Quote
Old 05-28-2009, 10:44 AM   #4 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: Toronto, ON
Posts: 111
Default

Code:
	
NSString *param = image.URL; // the URL from the XML
NSString *encodedParam = [param stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat: @"%@", param];
NSURL *url = [NSURL URLWithString:urlString];
UIImage *image = [[UIImage imageWithData: [NSData dataWithContentsOfURL: url]] retain];
finalImage = image;
CanadaDev is offline   Reply With Quote
Old 09-02-2009, 05:29 AM   #5 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 1
Default

Hey everyone,

I have the same Problem with th display images from XML.
Already everything tried. can help their me?

accusing for my bad English ;-)

Thank you!

Here is the Code:

Parser Code

Code:
-(void)parsXMLFileAtURL:(NSString*)URL

{

tutorials  = [[NSMutableArrayalloc]init];

     NSURL* xmlURL = [NSURL URLWithString:URL];

xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:xmlURL];

     [xmlParser setDelegate:self];

     [xmlParser parse];

     [xmlURL release];

}



- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict

{

currentElement = [elementName copy];

     if([elementName isEqualToString:@"newsticker"])

     {

item  = [[NSMutableDictionaryalloc]init];

currentTitle  = [[NSMutableStringalloc]init];

currentLink  = [[NSMutableStringalloc]init];

currentImage  = [[NSMutableStringalloc]init];

          

     }

}



- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

{

     if([elementName isEqualToString:@"newsticker"])

     {

          [item setObject :currentTitleforKey:@"title"];

          [item setObject :currentLinkforKey:@"subtitle"];

          [item setObject :currentImageforKey:@"image"];

          

          [tutorials addObject :[itemcopy]];

     }

}



- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

if([currentElement isEqualToString:@"title"])

     {

          [currentTitle appendString:string];

     }else if([currentElement isEqualToString:@"subtitle"])

     {

          [currentLink appendString:string];

          

     }else if([currentElement isEqualToString:@"image"])

     {

          [currentImage appendString:string];

     }

}



- (void)parserDidEndDocument:(NSXMLParser *)parser

{

     [self.tableView reloadData];

}
TableView

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



    static NSString *CellIdentifier = @"Cell";



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

          

          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

          cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

          cell.selectionStyle = UITableViewCellSelectionStyleGray;

          cell.textLabel.font = [UIFont fontWithName:kFonttextLabel size:18.0f];

          cell.detailTextLabel.textColor = [UIColor redColor ];

    }

     

     int tutorialIndex = [indexPath indexAtPosition:[indexPath length]-1];

     cell.textLabel.text = [[tutorials objectAtIndex:tutorialIndex] objectForKey:@"title"];

     cell.detailTextLabel.text = [[tutorials objectAtIndex:tutorialIndex] objectForKey:@"subtitle"];

     

     NSString* theURL = [[tutorials objectAtIndex:tutorialIndex] objectForKey:@"image"];

     NSLog(theURL);



UIImage *theImage = [UIImage imageWithData :[ NSDatadataWithContentsOfURL: [NSURLURLWithString: theURL]]];



     cell.imageView.image = theImage;



    return cell;

}
quischi79 is offline   Reply With Quote
Old 12-01-2009, 05:56 AM   #6 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 2
Default

Quote:
Originally Posted by CanadaDev View Post
Code:
	
NSString *param = image.URL; // the URL from the XML
NSString *encodedParam = [param stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat: @"%@", param];
NSURL *url = [NSURL URLWithString:urlString];
UIImage *image = [[UIImage imageWithData: [NSData dataWithContentsOfURL: url]] retain];
finalImage = image;
I got the same problem and when I tried CanadaDev code, it worked like a charm!

Thanks CanadaDev!
wizowsky is offline   Reply With Quote
Old 04-06-2010, 10:01 PM   #7 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 2
Default

I am attempting to do the same thing. Could you show me a completed example of just fetching an image from an XML file?
mbsaeger is offline   Reply With Quote
Old 06-14-2010, 12:45 PM   #8 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 11
Default

10010010000001111110000101100

Last edited by avanadra; 03-22-2011 at 10:55 AM.
avanadra is offline   Reply With Quote
Old 06-14-2010, 12:58 PM   #9 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 635
Default

Are you guys having trouble with parsing the xml or just creating the image once u have the url?
kapps11 is online now   Reply With Quote
Old 06-14-2010, 02:20 PM   #10 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 162
Default

Quote:
Originally Posted by mbsaeger View Post
I am attempting to do the same thing. Could you show me a completed example of just fetching an image from an XML file?
Quote:
Originally Posted by avanadra View Post
me too! Any luck, anyone?

There are numerous examples of how to parse a XML on the forum so I aint gonna reinvent the wheel here.

Once you get that done, you will have an array containing url, image link, title, etc..
you then apply CanadaDev code above to each url as you process the data in your array.

here's an example on how to do it http://imthi.com/blog/programming/ip...ource-code.php
__________________
Iphone Developers Exchange --> iPhone Dev Exchange
Iphone Dev Tweets --> iPhone Developers Tweets
web20devxer is offline   Reply With Quote
Old 07-05-2010, 08:49 PM   #11 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 2
Default

How do you implement canadadev's code? Like where do you put it?
Begna112 is offline   Reply With Quote
Old 07-16-2010, 08:59 PM   #12 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 3
Default

Any information on where CanadaDev's code goes?

I've looked at his sample and several others with different solutions to no avail so far.

Parsing, as a whole I'm covered.

It's just adding the images to the table view that has me vexed.

Help, please!
DangerWillRobinson is offline   Reply With Quote
Old 07-16-2010, 11:23 PM   #13 (permalink)
Senior Member
 
Join Date: Feb 2010
Location: dallas
Posts: 219
Default

try this
in cell
Code:
NSString *url1 = [NSString stringWithFormat:@"%@", aBook.image];//having image info
			NSString* url = [url1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
			
			
			NSLog(@"String = %@",url1);
			NSURL *imageURL = [NSURL URLWithString:url];
			
			NSLog(@"urlString = %@",imageURL);
			NSData *data =  [NSData dataWithContentsOfURL:imageURL];
			UIImage *image = [[UIImage alloc] initWithData:data];
			[imgView setImage:image];
gud4nuthin is offline   Reply With Quote
Old 07-17-2010, 02:47 PM   #14 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 3
Default

Quote:
Originally Posted by gud4nuthin View Post
try this
in cell
Code:
NSString *url1 = [NSString stringWithFormat:@"%@", aBook.image];//having image info
			NSString* url = [url1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
			
			
			NSLog(@"String = %@",url1);
			NSURL *imageURL = [NSURL URLWithString:url];
			
			NSLog(@"urlString = %@",imageURL);
			NSData *data =  [NSData dataWithContentsOfURL:imageURL];
			UIImage *image = [[UIImage alloc] initWithData:data];
			[imgView setImage:image];

Thanks, Gud4nuthin- I'm getting there!

I get no errors but my console log says

String = (null)
urlString = (null)

Does this mean the url i'm parsing doesn't have images? It does, so I assume I'm off somewhere else. Again, the articles parse and load fine, no problem.

// Set up the cell
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor colorWithRed:153.0/255.0 green:202.0/255.0 blue:60.0/255.0 alpha:1.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor colorWithRed:153.0/255.0 green:202.0/255.0 blue:60.0/255.0 alpha:1.0];

int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
[cell.textLabel setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
[cell.detailTextLabel setText:[[stories objectAtIndex: storyIndex] objectForKey: @"summary"]];
NSString *url1 = [NSString stringWithFormat:@"%@", currentImage];//having image info
NSString* url = [url1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


NSLog(@"String = %@",url1);
NSURL *imageURL = [NSURL URLWithString:url];

NSLog(@"urlString = %@",imageURL);
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:data];
[imageView setImage:image];
DangerWillRobinson is offline   Reply With Quote
Reply

Bookmarks

Tags
image, parsing, xml

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: 260
20 members and 240 guests
2WeeksToGo, ADY, BrianSlick, dacapo, Dani77, Dattee, Duncan C, headkaze, IphoneSdk, jemicha, kapps11, mer10, mgon987, Punkjumper, sneaky, timle8n1
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,879
Threads: 89,228
Posts: 380,743
Top Poster: BrianSlick (7,129)
Welcome to our newest member, mgon987
Powered by vBadvanced CMPS v3.1.0

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