2011-04-22 119 views
0

我通过传递xml参数从.net Web服务器获取图像。如何在表格视图顶部显示新图像

我越来越多的20个图像,我像这样一个接一个地在iPhone模拟器文档中保存图像。

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
     if([elementName isEqualToString:@"photo"]) 
     { 
      recordResults_photo = FALSE; 
      NSData *decode = [Base64 decode:resultData_photo]; 
      NSString *theXML = [[NSString alloc] initWithData:decode encoding:NSUTF8StringEncoding]; 
       NSData *decodedimage = [Base64 decode:theXML]; 
       UIImage *image = [UIImage imageWithData:decodedimage]; 
       NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
       NSLog(@"saving msg image %d",i); 
       NSString *pngFilePath = [NSString stringWithFormat:@"%@/image%d.png",docDir,i]; 
       NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)]; 
       [data1 writeToFile:pngFilePath atomically:YES]; 

    } 
} 

然后图像保存这样image0.png,image1.png,image2.png .....

,我在显示这样的表视图这些图像。

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

     NSString *pngFilePath = [NSString stringWithFormat:@"%@/image%d.png",docDir,indexPath.row]; 

     UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(250, 05, 30, 30)]; 
     img.image = [UIImage imageWithContentsOfFile:pngFilePath]; 
     [cell.contentView addSubview:img]; 

然后图像显示正常。

但现在我在这里有一个问题。

我从Web服务器获得一些更多的新图像,我需要将它们显示在表视图的顶部。

根据我的实施,我需要更改所有的图像名称。

这不是正确的方法,是否有任何替代方式或比我更好的方式。

任何人都可以帮助我。

感谢ü提前

回答

0

看到这个答案:

how to add new elements in array at the top of the table view

它比我的回答好得多了同样的问题,但双方会工作。

我刚刚确认,这确实将对象向下移动一个,所以这应该工作。

编辑:对不起,我误解了你的问题。至于更改图像名称,我看到可能的唯一方法是迭代数组,并在添加新对象后更改名称。

+0

但我怎么可以图像数组。 – Maheshbabu 2011-04-22 18:11:42

+0

你无法将UIImage添加到NSMutableArray? – 2011-04-22 18:13:36

+0

不,我不能够将UIImages添加到NSMutablearray – Maheshbabu 2011-04-22 18:31:01