异步图像加载性能

时间:2015-08-14 09:26:21

标签: ios objective-c iphone xcode uitableview

我在 UITableViewCell 中使用以下代码从url接收图像,一切正常但 UITableViewCell 中的图片加载速度非常慢,如果我认为是用户,这是不可接受的。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [checkData count];
}    

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger i =indexPath.row;
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIImageView propertyImage = (UIImageView *)[cell viewWithTag:10];
 NSString *test = [[checkData objectAtIndex:i]valueForKey:@"image"];
    CGRect frame=propertyImage.bounds;
    AsyncImageView asyImage=[[AsyncImageView alloc] initWithFrame:frame];
    [asyImage setImage:[UIImage imageNamed:@"loading.png"]];
    self.urlForTesting=[NSURL URLWithString:test];
    [asyImage loadImageFromURL:self.urlForTesting];
    asyImage.imageURL =self.urlForTesting;
    asyImage.contentMode = UIViewContentModeScaleToFill;
    asyImage.layer.masksToBounds=YES;
    [propertyImage addSubview:asyImage];
    return cell;
}

1 个答案:

答案 0 :(得分:0)

使用AFNetworking Class&添加另外两个文件,从github " UIImageView + AFNetworking" 下载并导入项目。然后从" cellForRowAtIndexPath"

添加此代码之后
// Fetch Image from URL
NSURL *url = [NSURL URLWithString:[[checkData objectAtIndex:i]valueForKey:@"image"]];
[objWinner.actWinner startAnimating];
[objWinner.actWinner setHidden:NO];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[objWinner.imgwinner setImageWithURLRequest:req placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
     [objWinner.actWinner stopAnimating]; // this is Activityindicator
     [objWinner.actWinner setHidden:YES];
     objWinner.imgwinner.image = image;
   } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
}];