Я пытаюсь загрузить какое-то изображение в свой UITableView с помощью ASIHTTP, но у меня есть некоторые проблемы. Прежде всего, я читаю файл xml (с tbxml) и сохраняю заголовок, путь к изображению и описание в словаре, а затем в массиве, для разбора я использую этот код:
- (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;
}