2

私はiPhoneアプリケーションで忙しいです。その一部として、Flickr から 100 個の小さな画像を UIButton として読み込みます。問題はメモリをどう扱うかです。毎回約5〜6 mbをロードします。ビュートラフのナビゲーションコントローラ popViewController の割り当てを解除すると、メモリはほぼ同じままです。

私が今やっていることは、すべての画像をループして、要求を ASINetworkQueue に入れることです。ループの準備ができたら、[networkQueue go] を実行します。

//Loop images
for (NSDictionary* message in output)
{
    NSString *imageURL = [[NSString alloc] initWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_s.jpg", [message objectForKey:@"farm"], [message objectForKey:@"server"], [message objectForKey:@"id"], [message objectForKey:@"secret"]];

    if(cx > 280) { cy = cy + 78; cx = 6.0f; }

    //user data for the request
    NSMutableDictionary *imageInfo = [[NSMutableDictionary alloc] init];

    //init Image
    CGRect myImageRect = CGRectMake(cx, cy, 72.0f, 72.0f);
    UIButton *myImage = [[UIButton alloc] initWithFrame:myImageRect];
    [myImage setTag:i];

    [myImage setBackgroundColor:[UIColor whiteColor]]; //grayColor
    [imageInfo setObject:[NSString stringWithFormat:@"%i", i] forKey:@"arrayNr"];
    [images addObject: myImage];
    [fotoView addSubview: myImage];

    //get url
    NSURL *imageURI = [[NSURL alloc] initWithString:imageURL];

    //load image
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL: imageURI];

    [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[message objectForKey:@"id"]]];
    [request setUserInfo: imageInfo];
    [networkQueue addOperation:request];

    [imageURL release];
    [imageURI release];

    [myImage release];
    [imageInfo release];

    cx = cx + 78;
    i++;
}

リクエストが終了したら、次のようにします。

- (void)imageFetchComplete:(ASIHTTPRequest *)request
{
    UIImage *img = [[UIImage alloc] initWithContentsOfFile:[request downloadDestinationPath]];
    if (img)
    {
        //set image
        UIButton *myButton = [images objectAtIndex:[[[request userInfo] objectForKey:@"arrayNr"] intValue]];
        [myButton setBackgroundImage:img forState: UIControlStateNormal];
        [myButton addTarget:self action:@selector(showImage:) forControlEvents:UIControlEventTouchUpInside];
    }

    [img release];
}

そして、これは私の dealloc 関数です:

- (void)dealloc
{
    [networkQueue cancelAllOperations];
    networkQueue.delegate = nil;
    [networkQueue release];

    [flickrController cancelAllOperations];
    flickrController.delegate = nil;
    [flickrController release];

    [images release];
    [fotoView release];
    [output release];

    [super dealloc];
}

記憶が消えない理由がわかりません。私が考えることができる解決策は次のとおりです。 - uibutton/uiimage 用の独自のオブジェクトを作成します。- UITableview を使用して画像を処理する

どちらのソリューションも、ビューを閉じた後にすべてのメモリをクリアする方法がわかりません。この種の状況に対処する方法を誰かが教えてくれることを願っています。

4

1 に答える 1

2

クリーンアップを行う場所にコードを投稿した場合、問題がどこにあるかがより明確になる可能性がありますが、とにかく推測しています。

このimageFetchComplete:メソッドでは、呼び出し時に画像への参照を保持します。setBackgroundImage:したがって、画像を保持していた特定のボタンオブジェクトを離すまで、画像はメモリに保持されます。ボタン自体は配列によって保持されているため、imagesこの配列も解放する必要があります。または、可変配列の場合は、ボタンを配列から削除します。

于 2010-07-12T11:17:13.593 に答える