2

バックグラウンド:

AppleのサンプルコードScrollViewSuiteから着想を得て、画像のサムネイルと1つの選択した画像を表示するViewControllerクラスを作成しました。「選択された」画像のコントロールの階層は次のようになります。

--> UIView
    --> UIScrollView
        --> UIImageView

UIScrollViewをビューに配置するには、次のコードを使用します。

imageScrollView = [[UIScrollView alloc] initWithFrame:frame];
[imageScrollView setBackgroundColor:[UIColor clearColor]];
[imageScrollView setDelegate:self];
[imageScrollView setBouncesZoom:YES];
[[self view] addSubview:imageScrollView];

...そして次のコードを使用してUIImageViewを構成し、UIScrollViewに追加します。

// Custom method to return a UIImage from a URL string
UIImage *image = [UIImage newImageWithContentsOfURL:imageURL];  

// first remove previous image view, if any
[[imageScrollView viewWithTag:MAIN_IMAGE_TAG] removeFromSuperview];

// set the new image view
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setDelegate:self];
[imageView setTag:MAIN_IMAGE_TAG];
[imageScrollView addSubview:imageView];
[imageScrollView setContentSize:[imageView frame].size];

// choose minimum scale so image width fits screen
float minScale  = [imageScrollView frame].size.width / [imageView frame].size.width;
[imageScrollView setMinimumZoomScale:minScale];
[imageScrollView setZoomScale:minScale];
[imageScrollView setContentOffset:CGPointZero];

// clear memory
[imageView release];
imageView = nil;

[image release];
image = nil;

URL文字列を使用してUIImageを取得するために使用したカテゴリメソッドは次のとおりです。

+ (UIImage *)newImageWithContentsOfURL:(NSString *)imageURL {   
    NSURL *url = [[NSURL alloc] initWithString:imageURL];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    UIImage *image = [[UIImage alloc] initWithData:data];
    [data release];
    [url release];

    return image;
}

問題: サイズ110 Kb(約)のjpegイメージをロードすると、アプリケーションの実際のメモリが12 MB(約)から38 MB(約)にジャンプします。これを最初に見たとき、私は困惑しました。これはどのように可能ですか?ええと、そして最終結果:アプリケーションはiPhone 3Gでクラッシュします(時々)。

メモリーの読み取りは、Instrumentsのメモリーモニターツールを使用して行われたことに注意してください-デバイス(シミュレーターではない)でアプリケーションをテストしている間。また、Instrumentsはメモリリークを示さず、StaticAnalyzerは疑わしいものを指し示していないことにも注意してください。

私は助けが必要です!

4

2 に答える 2

3

jpegが圧縮されているという事実と関係があるのでしょうか。表示時に圧縮解除されている可能性があるため、メモリが大幅に増加します。

1:1スケールでの画像の寸法はどれくらいですか?

于 2010-02-12T14:06:27.267 に答える
0

確かにそれはそれを非常に多くのメモリとクラッシュを使用させているjpeg以外のものでなければなりません-私は15200x250ピクセルのpngを持っていてそれは美しくスクロールします...

于 2010-07-10T08:23:52.963 に答える