【问题标题】:Async Image Loading Performance异步图像加载性能
【发布时间】:2015-08-14 09:26:20
【问题描述】:

我在 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;
}

【问题讨论】:

标签: ios objective-c iphone xcode uitableview


【解决方案1】:

使用 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){
}];

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
  • 2011-12-23
  • 2013-04-08
相关资源
最近更新 更多