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 秒を失います。 UIImageView を bigImage で初期化してからこの 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;
}
}