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 = nil;
}

每张图像为 640 x 820。

4

4 回答 4

2

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

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

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

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

-您提前知道要加载哪些资产。

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

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

于 2012-04-04T16:11:46.837 回答
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];
 } 
于 2012-04-04T15:46:46.207 回答
0

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

于 2012-04-04T15:48:18.753 回答
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) 在后台加载图像。

于 2012-04-04T16:25:25.723 回答