找到你要的答案

Q:Async Image Loading Performance

Q:异步图像加载性能

I am loading images in UITableViewCell receiving from url using below code, everything is working fine but the image load very slow in UITableViewCell and if i think as a user, it is not acceptable.

- (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;
}

我加载图像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;
}
answer1: 回答1:

Use AFNetworking Class & add additional two file to download from github "UIImageView+AFNetworking" and import in your project. then after add this code from "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){
}];

使用AFNetworking类&;添加额外的两个文件从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){
}];
ios  objective-c  iphone  xcode  uitableview