开发者

How to print xml image in IOS?

I've created a simple XML-Parser to parse my rss Feeds to my app. The id, currentstatus, picture of the simple to parse from the xml file. But I can't get the image from the XML file. . i retrieve the all the images from xml file. but the noimage.jpg not displaying. could you please any help me.

XML code

<Agent>
<id>3422</id>
<currentstatus>Logged Off</currentstatus>
<picture>1</picture>
</Agent>
<Agent>
<id>3432</id>
<currentstatus>Logged Off</currentstatus>
<picture>0</picture>
</Agent>

i tried the following code:

UIImage * cellimages = [[UIImage alloc]init];   
NSString *string1开发者_如何学编程 = @"http://telecomstats.co.uk/images/LLReaderProfile/";
NSString *cellvalue =[[[[self rssParser]rssItems]objectAtIndex:indexPath.row]picture];
NSString* disable = [[NSString alloc] initWithString:@"0"];


if (cellvalue == disable)
{
 cellimages = [UIImage imageWithContentsOfFile:@"http://telecomstats.co.uk/images/LLReaderProfile/noimage.jpg"];
} 
else     {

    cellimages = [string1 stringByAppendingString:[[[[self rssParser]rssItems]objectAtIndex:indexPath.row]id]];
    cellimages = [cellimages stringByAppendingString: @".jpg"];  

}

cellimages = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:cellimages]]];  
cell.imageView.image = cellimages;

Does someone can help me? Thanks for any help and insight.


You're using cellimages which is an UIImage object to store strings in your else-branch. Also when cellvalue == disable is true, you execute this:

cellimages = [UIImage imageWithContentsOfFile:@"http://telecomstats.co.uk/images/LLReaderProfile/noimage.jpg"];
cellimages = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:cellimages]]];  

There you put an UIImage in [NSURL URLWithString:] which probably will not work; actually this code should produce quite some warnings. Try to properly separate between the URL string and the image object to display. Put all URL stuff in an NSString and get the UIImage afterwards when you have determined the correct URL.

Note that your code has some other problems, especially that you are using dataWithContentsOfURL and imageWirhContentsOfFile both of which will block until the image is retriebes. This will result in application hangs when using a bad internet connection and if the network is too slow, will crash your app. Have a look into asynchronous downloads and using frameworks like AFNetworking.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜