バックグラウンド:
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は疑わしいものを指し示していないことにも注意してください。
私は助けが必要です!