2014-04-22 198 views
0

我有一個UITableView,它從服務器上下載其UITableViewCell的圖像。帶圖像的UITableview滾動非常緩慢

我發現表格滾動速度很慢。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"parallaxCell"; 
    JBParallaxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    NSURL *imageURL = [NSURL URLWithString:[[news objectAtIndex:indexPath.row]objectForKey:@"Resim"]]; 
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 
    UIImage *imageLoad = [[UIImage alloc] initWithData:imageData]; 


    cell.titleLabel.text = [[news objectAtIndex:indexPath.row]objectForKey:@"Adi"]; 
    cell.subtitleLabel.text = [[news objectAtIndex:indexPath.row]objectForKey:@"Resim"]; 
    cell.parallaxImage.image = imageLoad; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    return cell; 
} 
+1

使用https://github.com/rs/SDWebImage懶懶地加載你的圖片。 – limon

回答

1

您正在主線程上加載圖像文件,並且此操作會減慢滾動速度。使用來自AFNetworking的UIImageView+AFNetworking.h,通過異步圖像加載來加速您的應用程序。鏈接https://github.com/AFNetworking/AFNetworking

0

我使用這個庫這僅僅是完美

你只需要#import <SDWebImage/UIImageView+WebCache.h>到您的項目,當圖像正在下載時,您還可以定義佔位符代碼只是這個代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    // Here we use the new provided setImageWithURL: method to load the web image 
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
        placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    cell.textLabel.text = @"My Text"; 
    return cell; 
} 

它也緩存下載的圖像,併爲您提供良好的性能。

希望它能幫助你!

+0

謝謝Erid Works Perfect – SeRcCaN

+0

我很高興它可以幫助你!您可以通過選中此答案來投票選出答案並設置正確的答案! :) – EridB