Iphone UITableView的异步下载问题&;阿西特
我尝试使用ASIHTT在UITableView上加载一些图像,但遇到了一些问题。首先,我读取一个xml文件(使用tbxml),然后将标题、图像路径和描述保存在字典中,然后保存在数组中,为了进行解析,我使用以下代码:Iphone UITableView的异步下载问题&;阿西特,iphone,uitableview,asynchronous,asihttprequest,Iphone,Uitableview,Asynchronous,Asihttprequest,我尝试使用ASIHTT在UITableView上加载一些图像,但遇到了一些问题。首先,我读取一个xml文件(使用tbxml),然后将标题、图像路径和描述保存在字典中,然后保存在数组中,为了进行解析,我使用以下代码: - (void)loadUnknownXML { // Load and parse the books.xml file tbxml = [TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.xxx.co
- (void)loadUnknownXML {
// Load and parse the books.xml file
tbxml = [TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.xxx.com"]];
// If TBXML found a root node, process element and iterate all children
if (tbxml.rootXMLElement){
[self traverseElement:tbxml.rootXMLElement];
}
}
- (void) traverseElement:(TBXMLElement *)element {
TBXMLElement *child = element->firstChild;
TBXMLElement *items = [TBXML childElementNamed:@"item" parentElement:child];
do{
if (items->firstChild) {
TBXMLElement *titolo = [TBXML childElementNamed:@"title" parentElement:items];
TBXMLElement *descrizione = [TBXML childElementNamed:@"description" parentElement:items];
//NSLog(@"Titolo: %@ \n Descrizione: %@",[TBXML textForElement:titolo],[TBXML textForElement:descrizione]);
self.elemento = [[NSMutableDictionary alloc] init];
[self.elemento setObject:[TBXML textForElement:titolo] forKey:@"Titolo"];
NSString *indirizzoImmagine = [TBXML textForElement:descrizione];
NSRange rangeSRC = [indirizzoImmagine rangeOfString:@"src=\""];
indirizzoImmagine = [indirizzoImmagine substringFromIndex:NSMaxRange(rangeSRC)];
NSRange rangeAMP = [indirizzoImmagine rangeOfString:@"&"];
NSRange rangeWidth = [indirizzoImmagine rangeOfString:@"&width"];
if (rangeAMP.location != NSNotFound) {
indirizzoImmagine = [indirizzoImmagine substringToIndex:NSMaxRange(rangeAMP)];
}
else if (rangeWidth.location != NSNotFound){
indirizzoImmagine = [indirizzoImmagine substringToIndex:NSMaxRange(rangeWidth)];
}
indirizzoImmagine = [indirizzoImmagine stringByReplacingOccurrencesOfString:@"&" withString:@""];
indirizzoImmagine = [indirizzoImmagine stringByReplacingOccurrencesOfString:@"&width" withString:@""];
[self.elemento setObject:indirizzoImmagine forKey:@"IndirizzoImmagine"];
[self.elemento setObject:[TBXML textForElement:descrizione] forKey:@"Descrizione"];
[self.array addObject:self.elemento];
}
}
while ((items=items->nextSibling));
}
然后我从下载开始
- (void) loadURL:(NSURL *)url index:(int)index
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
ASIDownloadCache *cache = [[ASIDownloadCache alloc] init];
[request setDownloadCache:cache];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[cache setStoragePath:@"/Users/kikko/kikko/xxx"];
request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:index], @"index",
url, @"url", nil];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
int index = [[request.userInfo valueForKey:@"index"] intValue];
ASIDownloadCache *cache = [[ASIDownloadCache alloc] init];
[cache setStoragePath:@"/Users/kikko/kikko/xxx"];
[request.userInfo valueForKey:@"url"];
if ([cache cachedResponseDataForURL:[request.userInfo valueForKey:@"url"]]==nil) {
NSLog(@"%@",[request.userInfo valueForKey:@"url"]);
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:index inSection:0];
NSArray* rows = [NSArray arrayWithObjects:indexPath, nil];
[table reloadRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationNone];
}
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"Error: %@",error);
}
最后,我将图像放入cell.imageview中
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
-(NSString *) stringByStrippingHTML:(NSString *)stringa {
NSRange r;
NSString *str = stringa;
while ((r = [str rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
str = [str stringByReplacingCharactersInRange:r withString:@""];
return str;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
elemento = [array objectAtIndex:indexPath.row];
NSURL *indirizzoImmagine = [NSURL URLWithString:[elemento objectForKey:@"IndirizzoImmagine"]];
[self loadURL:indirizzoImmagine index:indexPath.row];
ASIDownloadCache *cache = [[ASIDownloadCache alloc] init];
[cache setStoragePath:@"/Users/kikko/kikko/xxx"];
dataImmagine = [cache cachedResponseDataForURL:indirizzoImmagine];
[cell.imageView setImage:[UIImage imageWithData:dataImmagine]];
cell.textLabel.text = [elemento objectForKey:@"Titolo"];
return cell;
-(NSInteger)节数表视图:(UITableView*)表视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回self.array.count;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径
{
返回50;
}
-(NSString*)stringByStrippingHTML:(NSString*)stringa{
NSRange;
NSString*str=stringa;
while((r=[str rangeOfString:@“]+>”选项:NSRegularExpressionSearch])。位置!=NSNotFound)
str=[str STRINGBYREPLAcingCharactersRange:r with string:@'';
返回str;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“ItemCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(!单元格){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier];
}
elemento=[array objectAtIndex:indexath.row];
NSURL*INDIRIZOIMGINE=[NSURL URLWithString:[elemento objectForKey:@“INDIRIZOIMGINE”];
[self-loadURL:IndirizzoimEngine索引:indexPath.row];
ASIDownloadCache*cache=[[ASIDownloadCache alloc]init];
[缓存设置存储路径:@”/Users/kikko/kikko/xxx];
DataImEngine=[cache CachedResponsaForUrl:IndirizzoImEngine];
[cell.imageView setImage:[UIImage-imageWithData:dataImmagine]];
cell.textlab.text=[elemento objectForKey:@“Titolo”];
返回单元;
}苹果公司给出了一个下载和显示图像的最佳例子
同时,我也将检查您的代码有很多方法可以做到这一点,但对现有代码最简单的方法是将userinfo字典附加到您的请求对象 在遍历方法中,请执行以下操作:
//...
while ((items=items->nextSibling));
for (int i = 0; i < [self.array count]; i++)
{
[arrayData addObject:[NSNull null]];
}
在单元格代码中,执行以下操作:
if ([arrayData objecAtIndex:indexPath.row] != [NSNull null]) {
UIImage *img = [UIImage imageWithData:[self.arrayData objectAtIndex:indexPath.row]];
imageView.image = img;
[cell.imageView setImage:img];
} else {
imageView.image = nil;
}
非常感谢,我还有另一个问题,用这种方式获取图像我必须向下滚动然后向上滚动,如何在下载图像时重新加载显示图像的表格数据?编辑答案。在requestFinished方法中,您需要调用reloadRowsAtIndexPath::根据列表的大小,您将遇到内存问题。而不是将图像存储在数组中。。。您应该直接从cellForRowAtIndexPath方法调用loadUrl:index:method,并在返回时使用ReloadRowSatineXPath调用。您可以完全跳过将其添加到arrayData。你缓存了图像,所以第二次应该会很快。事实上,我花了很多内存,:(我有大约200/300个项目,我不明白我该怎么做你说的?我还有一个问题,表格有200/300个项目,但是当我破坏表格时,我只显示10/15个项目,然后我显示e“加载更多…”如果我在创建和单击“加载更多”时重新加载一个表,这可能是一个好主意?完全去掉arrayData,而是从ASI下载缓存中获取图像。有一种方法可以直接从缓存中获取图像文件。
if ([arrayData objecAtIndex:indexPath.row] != [NSNull null]) {
UIImage *img = [UIImage imageWithData:[self.arrayData objectAtIndex:indexPath.row]];
imageView.image = img;
[cell.imageView setImage:img];
} else {
imageView.image = nil;
}