2012-04-04 56 views
0

嗨我一直有问题快速加载图像到屏幕上。似乎有一个延迟,我第一次打的看法:iphone - image loading

这是我如何加载图片:

for (int i = 1; i < 19; i++) 
{ 
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_Hole%d_", courseName, i]]; 
    imageView = [[UIImageView alloc] initWithImage:image]; 
    frame.origin.x = cx; 
    imageView.frame = frame; 
    [scrollView addSubview:imageView]; 
    cx += scrollView.frame.size.width; 
    imageView = nil; 
} 

每个图像为640 x 820

+0

为什么你不希望加载19个640x820的图像需要一点时间来点击你的视图?另外,我会关心所有这些图像的内存使用情况。你有没有想过仅仅在屏幕上需要延迟加载图像? – 2012-04-04 16:00:28

回答

0

我不认为有你可以做很多其他事情来加快速度。

但是,您确实有内存管理问题,可能会在某种程度上加快进程。

for (int i = 1; i < 19; i++)  { 
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_Hole%d_", courseName, i]]; 
    imageView = [[UIImageView alloc] initWithImage:image]; 
    frame.origin.x = cx; 
    imageView.frame = frame; 
    [scrollView addSubview:imageView]; 
    cx += scrollView.frame.size.width; 
    [imageView release]; 
} 
+2

如果OP使用ARC,那么调用'release'是一个错误,原始代码在内存管理方面没有问题。 – 2012-04-04 15:48:10

0

是的,加载图像需要一些时间。你有大量的大图像,所以这是预期的结果。 尝试考虑平铺图层,而不是将UIImages添加到滚动视图。 CATiledLayer可能会帮助您

+0

任何可以帮助我的例子? – Mikerizzo 2012-04-04 16:04:50

2

正如其他人已经注意到您正在遇到一个基本的图像处理问题:设备的有限I/O速度。

顺便提一句,imagedNamed:内部缓存图像,这可能解释为什么后续加载速度很快。

如果平铺不是一个选项(如@Krio推荐),您可以尝试在需要查看视图之前将图像预加载到后台线程缓存中。例如,如果此视图通常是从另一个视图访问的,则可以在加载较早视图时启动一系列在dispatch_asyncNSOperationQueue中打包的imageNamed:调用。但这只会在某些情况下有所帮助:

- 您知道您的图像视图几乎总是要请求的下一个UI元素。

- 您事先知道哪些资产需要加载。

- 您的图像集是不够,你不打算填补了所有的记忆,迫使框架来赶你刚刚填写

注意,这也增加了复杂性相当数量的高速缓存小你的应用程序设计,所以应该尽量避免,除非绝对必要。

0

嗨图像加载冻结用户界面。您可以在后台执行此任务,以便UI永不结合。尝试下面的代码。

-(void)loadImages{ 
     [self performSelectorInBackground:@selector(loadImagesInBackGround) withObject:nil]; 
    } 

    -(void)loadImagesInBackGround{ 

     for (int i = 1; i < 19; i++) 
    { 
     UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_Hole%d_", courseName, i]]; 
     imageView = [[UIImageView alloc] initWithImage:image]; 
     frame.origin.x = cx; 
     imageView.frame = frame; 
     [scrollView addSubview:imageView]; 
     cx += scrollView.frame.size.width; 
     imageView = nil; 
    } 

} 

你应该调用loadImages:你要从哪里加载图像。或者,您可以尝试Grand Central Dipatch(GCD)将图像加载到背景中。