UIImageView与大图像。问题

时间:2012-06-25 13:33:32

标签: iphone ios image uiimageview lazy-loading

我想实现iPhone Photo App(默认的iPhone应用程序)。当我想加载大图像(2500 * 3700)时,我遇到了困难。当我想从一个图像滚动到另一个图像时,我看到一些像口吃的东西。要显示图像,我使用Apple网站上的ImageScrollView它有显示方法:ImageScrollView.m

- (void)displayImage:(UIImage *)image
{
    // clear the previous imageView
    [imageView removeFromSuperview];
    [imageView release];
    imageView = nil;

    // reset our zoomScale to 1.0 before doing any further calculations
    self.zoomScale = 1.0;
    self.imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
    [self addSubview:imageView];

    self.contentSize = [image size];
    [self setMaxMinZoomScalesForCurrentBounds];
    self.zoomScale = self.minimumZoomScale;
}

- (void)setMaxMinZoomScalesForCurrentBounds
{
    CGSize boundsSize = self.bounds.size;
    CGSize imageSize = imageView.bounds.size;

    // calculate min/max zoomscale
    CGFloat xScale = boundsSize.width / imageSize.width;    // the scale needed to perfectly fit the image width-wise
    CGFloat yScale = boundsSize.height / imageSize.height;  // the scale needed to perfectly fit the image height-wise
    CGFloat minScale = MIN(xScale, yScale);                 // use minimum of these to allow the image to become fully visible

    // on high resolution screens we have double the pixel density, so we will be seeing every pixel if we limit the
    // maximum zoom scale to 0.5.
    CGFloat maxScale = 1.0 / [[UIScreen mainScreen] scale];

    // don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.) 
    if (minScale > maxScale) {
        minScale = maxScale;
    }

    self.maximumZoomScale = maxScale;
    self.minimumZoomScale = minScale;
}

对于我的应用程序,我有600 * 600张图像,我先显示它们。当用户滚动到下一个图像时,他只能看到600 * 600图像。然后在后台我加载3600 * 3600图像

[operationQueue addOperationWithBlock:^{
    UIImage *image = [self getProperBIGImage];
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        ImageScrollView *scroll = [self getCurrentScroll];
        [scroll displayImage:newImage];
    }];
}];

我看到,当图像尺寸为3600 * 3600并且我想在640 * 960屏幕上显示图像时,iPhone会浪费1秒的主队列时间来缩放图像,这就是为什么我无法滚动到这1秒内的下一张图片。

我想缩放图片,因为我需要用户才能缩放此图片。我尝试使用这种方法,但这没有帮助。

我看到了一些可能的解决方案:

1)在后台提供UIImageView中的图像缩放(但我知道,UI只应在主线程中更改)

2)使用 - (UIView *)viewForZoomingInScrollView :( UIScrollView *)scrollViewCalled在开始时只显示600 * 600图像然后加载大图像,当用户尝试缩放时(但我试过这个,我会松开1秒,当我尝试用bigImage初始化UIImageView然后返回这个UIImageView;而我甚至无法实现它,因为我看到滚动视图不好,滚动行为错误(难以解释),当我尝试返回不同不同尺度的视图)

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollViewCalled
{
    if (!zooming)
    {
        ImageScrollView *scroll = (ImageScrollView *)scrollViewCalled;
        UIImageView *imageView = (UIImageView *)[scroll imageView];
        return imageView;
    }
    else
    {
         UIImageView *bigImageView = [self getBigImageView];
         return bigImageView;
     }
}

1 个答案:

答案 0 :(得分:1)

不幸的是,该图片对于iPhone来说太大了。我知道在iPad上,限制大致是设备屏幕的大小。任何大于那个,你将不得不使用CATiledLayer。

从2010年开始,我将在过去三年中看一下WWDC UIScrollView演示文稿。在那里,他们讨论了在iPhone上处理大图像的方法。示例代码也可以帮助您。

祝你好运!