我想实现 iPhone Photo App(默认 iPhone 应用)。当我想加载大图像(2500 * 3700)时,我遇到了困难。当我想从一个图像滚动到另一个图像时,我会看到类似口吃的东西。要显示图像,我使用苹果网站的 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;
}
}