2010-11-01 73 views
0

我试图实现约30个教程今天,只是不能得到任何工作。桌面视图与图像,缓慢加载和滚动

我的问题是,我通过JSON文件加载我的信息,将数据添加到NSMutableArray,然后使用表来显示它。它工作正常,当我没有图像,但是当我做它的负载非常缓慢,滚动非常粘。今天的调查结果显示,每次滚动都会重新加载图像,这就是为什么它很慢的原因。

有人可以分解它,让我更容易解决这个问题吗?

亚历

回答

0

你还挺留下您的问题敞开B/C你不够具体。性能问题可能与一堆事情有关。 以下是一些表格单元格的性能问题&图片

•在后台线程上加载图像。

•重用的小区 - 不分配任何比你更需要在屏幕上

static NSString *CellIdentifier = @"Cell"; 

    CellClass *cell = (CellClass*)[tv dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) cell = [[[CellClass alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 

•仅绘制的单元格的大小相同的图像(即,如果一个单元是44像素高,保持在44px的UIimages)。如果图像较大,则可能需要在从互联网上下载图像后处理这些图像。

•请勿在您的手机中使用uiimageview。而是创建一个自定义单元格(即。子类)并在drawRect:函数中绘制图像。

1

看看Apple's LazyTableImages example。基本上它归结为

一)重用你的表格单元格

b)只图像加载当前可见

+0

嗨,谢谢,是的,我看着这个,发现它很难实现到我的应用程序,因为它使用rss提要等我googled重新表和加载图像是可见的,你建议,发现这很酷完美的教程ct http://www.markj.net/iphone-asynchronous-table-image/ 谢谢 – Alexj17 2010-11-04 13:37:46

0

您应该使用在AFNetworkingSDWebImage发现UIImageView类别提供异步图像检索。这些类别:

  • 是非常容易使用(而不是使用UIImageView方法setImage,而是使用的类别setImageWithURL方法之一);

  • 提供异步图像检索;

  • NSCache缓存下载的图像,以确保您不必检索刚刚下载的图像;

  • 确保您的用户界面无法得到积压下载已经滚动屏幕的单元格的图像;和

  • 利用操作队列来约束并发度(而不是使用可导致超时失败的GCD全局队列)。

0

我有一个类,我叫RemoteImageHandler。这里是 。.h文件:

#import <UIKit/UIKit.h> 

@interface RemoteImageHandler : NSObject 

- (void)imageForUrl:(NSURL*)url callback:(void(^)(UIImage *image))callback; 

+ (RemoteImageHandler *)shared; 

@end 

与.m文件:

#import "RemoteImageHandler.h" 

@interface RemoteImageHandler() 

@property (nonatomic, strong) NSMutableDictionary *imageDictionary; 

@end 

@implementation RemoteImageHandler 

- (void)imageForUrl:(NSURL*)url callback:(void(^)(UIImage *image))callback { 
    if (!!self.imageDictionary[url]) { 
     callback(self.imageDictionary[url]); 
    } else { 
     dispatch_async(dispatch_get_global_queue(0,0), ^{ 
      NSData * data = [[NSData alloc] initWithContentsOfURL:url]; 
      if (data == nil) 
       callback(nil); 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       UIImage *image = [UIImage imageWithData:data]; 
       self.imageDictionary[url] = image; 
       callback(image); 
      }); 
     }); 
    } 
} 

+ (TQRemoteImageHandler *)shared { 
    static TQRemoteImageHandler *shared = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     shared = [[self alloc] init]; 
    }); 
    return shared; 
} 

@end 

在我的表视图,每当我想从远程位置的图像(让我们说这是的cellForRowAtIndexPath,我用这个:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath]; 

    [[RemoteImageHandler shared] imageForUrl:someURLCorrespondingToTheImageYouWant callback:^(UIImage *image) { 
     cell.imageView.image = image; 
    }]; 

    return cell; 
}