0

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;
     }
}
4

1 に答える 1

1

残念ながら、その画像は iPhone で処理するには大きすぎます。iPad では、制限はおおよそデバイスの画面のサイズです。それより大きい場合は、CATiledLayer を使用する必要があります。

2010 年からの過去 3 年間の WWDC UIScrollView プレゼンテーションを見てみましょう。その中で、彼らは iPhone で大きな画像を処理する方法について議論しています。サンプル コードも参考になります。

幸運を!

于 2012-08-20T14:39:55.413 に答える