[英]Parsing XML - loading images from URLs
我正在解析一個具有圖片鏈接的XML文件,並試圖在UITableView中顯示它們。 由於某些原因,它們沒有出現在視圖中。 我認為問題與解析腳本無關,因為它可以在表中顯示文本。 這是我顯示圖片的代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
Tweet *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
CGRect imageFrame = CGRectMake(2, 8, 40, 40);
UIImageView *customImage = [[UIImageView alloc] initWithFrame:imageFrame];
customImage.tag = 0013;
[cell.contentView addSubview:customImage];
}
UIImageView *customImage = (UIImageView *)[cell.contentView viewWithTag:0013];
customImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentTweet pic]]]];
return cell;
}
我是否必須首先加載圖像? 如果是這樣,由於解析xml后僅獲取圖像的網址,該怎么辦?
感謝任何幫助Antoine
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
Tweet *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
UIImageView *customImage = (UIImageView*)[cell.contentView viewWithTag:1234];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
CGRect imageFrame = CGRectMake(2, 8, 40, 40);
customImage = [[UIImageView alloc] initWithFrame:imageFrame];
customImage.tag = 1234;
[cell.contentView addSubview:customImage];
}
dispatch_queue_t imageQueue = dispatch_queue_create("queue", NULL);
dispatch_async(imageQueue, ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentTweet pic]]]];
dispatch_async(dispatch_get_main_queue(), ^{
customImage.image = image;
});
});
return cell;
}
@interface ViewController ()
{
NSMutableArray *images;
NSMutableDictionary *dicImages_msg;
BOOL isDecliring_msg;
BOOL isDragging_msg;
}
@implementation ViewController ()
- (void)viewDidLoad
{
[super viewDidLoad];
dicImages_msg = [[NSMutableDictionary alloc] init];
[self parsing];//parse and save the images to the images array
}
- (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];
// Set up the cell...
}
cell.imageView.image=[UIImage imageNamed:@"placeholder.png"]; //placeholder image is blank image displayed till the image loads
if ([dicImages_msg valueForKey:[images objectAtIndex:indexPath.row]])
{
if (images.count == 0)
{
cell.imageView.image=[UIImage imageNamed:@"placeholder.png"];
}
else
{
cell.imageView.image=[dicImages_msg valueForKey:[images objectAtIndex:indexPath.row]];
}
}
else
{
if (!isDragging_msg && !isDecliring_msg)
{
[self performSelectorInBackground:@selector(downloadImage_3:) withObject:indexPath];
}
else
{
cell.imageView.image=[UIImage imageNamed:@"placeholder.png"];
}
}
return cell;
}
-(void)downloadImage_3:(NSIndexPath *)path
{
NSAutoreleasePool *pl = [[NSAutoreleasePool alloc] init];
UIImage *img=[[UIImage alloc]init];
img=[UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:path.row]]]];
[dicImages_msg setObject:img forKey:[images objectAtIndex:path.row]];
[self.YourTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
[pl release];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.