iphone - 图像加载

时间:2012-04-04 15:42:37

标签: iphone ios xcode4.2

您好我在将图像快速加载到屏幕时遇到了问题。第一次看到视图时似乎有延迟:

以下是我加载图片的方式:

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。

4 个答案:

答案 0 :(得分:2)

正如其他人所说,您遇到了图像处理的基本问题:设备的有限I / O速度。

顺便说一下,imagedNamed:在内部缓存图像,这可能解释了后续加载速度快的原因。

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

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

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

- 你的一组图像足够小,你不会填满所有内存,迫使框架逐出你刚刚填充的缓存

请注意,这也会给您的应用程序设计带来相当大的复杂性,因此除非绝对必要,否则最好避免使用。

答案 1 :(得分: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 :(得分:0)

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

答案 3 :(得分:0)

嗨图像加载会冻结UI。您可以在后台执行此任务,以便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)在背景中加载图像。