2011-02-03 14 views
0

こんにちは私はTwitterで作業しています。画像に問題があります。ツイートの表示にUITableviewがあります。ツイートにはユーザー写真が入っています。 私はUITableviewをスクロールすると非常にゆっくりとスクロールします。写真のメモリサイズを減らして、スクロールすることを提案してください。iPhone:ピクチャのメモリサイズを減らす方法

私はサムネイルはそれをしない、メモリサイズを小さくすることができます聞いた。このコードでそれを行う方法もしそうなら(私は選択して、どのようなサムネイル方法を行う者のために持っている方法ではない場合)(テーブルビューセルコード)

//Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *identifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 


    if(!cell) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:nil] autorelease]; 

    UIImageView *myImage = [[UIImageView alloc]initWithFrame:CGRectMake(6,10,58,60)] ; 

    NSURL *url = [NSURL URLWithString:[(Tweet*)[tweetArray objectAtIndex:indexPath.row] image_url]]; //here Tweet is other class and image_url is method in the Tweet class 

    NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 

    [myImage setImage: [UIImage imageWithData:data]]; 

    [cell.contentView addSubview:myImage]; 

    [myImage release]; 

    } 

    return cell; 

} 

は、(スレッドなしでテーブルのイメージをダウンロードしてください)

くれ

+0

コードを強調するために、あなたの投稿を編集してください。このように読むのは不可能です。 –

答えて

1

テーブルビューをスロースクロールする理由は、メインスレッドのすべてのセルのイメージデータを取得しようとしているためです。 URLから画像データを取得しようとしている間にUIがブロックされています。私によれば、テーブルビューも読み込まれている間はメインスレッドで画像をダウンロードできません。

このアプローチを使用する代わりに、NSOperationQueue、NSOperationおよびNSThreadを使用して、適切なセルへのイメージの非同期ロードを行う必要があります。

あなたは画像の非同期をダウンロードするには2-3関数のように詳細なヘルプや単純なコードが必要な場合は...ここで

は、関数がある....あなたが値だけコールを取得/解析している

[self startLoading]; UIをブロックせずに画像をロードします。

- (void) startLoading { 
    NSOperationQueue *queue = [[[NSOperationQueue alloc]init]autorelease]; 
    NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(loadImagesInBackground) object:nil]; 
    [queue addOperation:op]; 
    [op release]; 
} 

-(void) loadImagesInBackground { 
     int index = 0; 
     for (NSString urlString in [(Tweet*)[tweetArray objectAtIndex:indexPath.row] image_url]) { 
      NSURL *url = [NSURL URLWithString:urlString]; 
      NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 
      [myImageArray addObject: [UIImage imageWithData:data]]; 
      index++; 
      if(index/3==0) 
      [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; 
     } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *identifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 


    if(!cell) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:nil] autorelease]; 

    UIImageView *myImage = [[UIImageView alloc]initWithFrame:CGRectMake(6,10,58,60)] ; 

    [myImage setImage: [myImageArray objectAtIndex:indexpath.row]]; 

    [cell.contentView addSubview:myImage]; 

    [myImage release]; 

    } 

    return cell; 

} 
+0

+1のバックグラウンド読み込み。 ASIHTTPRequestが非常に少ない作業でこれほど多くを行うようになると、NSOperationQueueとその仲間にとって-1です。ネット0 ;-) –

0

This articleを提案してください、ありがとうあなたの答えを持っているかもしれません。 iOS開発ライブラリにはthis sampleもあります。このライブラリには、できるだけ早く画像を非同期で読み込む方法が示されています。

0

確かに、他の回答者が説明したように、画像をゆっくりと非同期でダウンロードしたいとします。

サムネイルサイズに合わせてサイズを変更し、小さいサイズのメモリに保存することもできます。チュートリアルとそれを行うための実際の使用可能なライブラリコードについては、this articleを参照してください(コピー&ペーストの開発に関する噂をスクロールダウンしてください)。

編集:あなたは明らかにバックグラウンドダウンロードの部分でもっと助けが必要です。ここで私はそれをやっている。

インストールASIHTTPRequestは、HTTPクライアントの作業を大幅に簡素化するサードパーティライブラリです。指示に従ってプロジェクトにインストールし、.mファイルの先頭に#include "ASIHTTPRequest.h"を入れてください。あなたのtableView: cellForRowAtIndexPath:で次に

、行く:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *identifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 


    if(!cell) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 

     UIImageView *myImage = [[UIImageView alloc]initWithFrame:CGRectMake(6,10,58,60)] ; 

     [cell.contentView addSubview:myImage]; 
     [myImage release]; 
    } 

    // so at this point you've got a configured cell, either created fresh 
    // or dequeued from the table's cache. 

    // what you DON'T have is a pointer to the uiimageview object. So we need to 
    // go get it. 

    UIImageView *theImageView; 
    for (UIView *view in cell.contentView.subviews) { 
     if ([view isKindOfClass:[UIImageView class]) { 
      theImageView = view; 
      break; 
     } 
    } 

    NSURL *url = [NSURL URLWithString:[(Tweet*)[tweetArray objectAtIndex:indexPath.row] image_url]]; //here Tweet is other class and image_url is method in the Tweet class 

    ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:url]; 
    [req setCompletionBlock:^{ 
     NSData *data = [req responseData]; 

     // This is the UIImageView we extracted above: 
     [theImageView setImage: [UIImage imageWithData:data]]; 
    }]; 

    // this will start the request in the background, and call the above block when done. 
    [req startAsynchronous]; 

    } 

    // Then return the cell right now, for the UITableView to render. 
    // The image will get filled in later when it returns from the server. 
    return cell; 

} 
+0

こんにちはDanRayは、写真のメモリサイズを小さくするためにサムネイルを使用しています。小さなピクチャサイズにはちょうど大きな写真のサイズを小さくしています。ありがとうございます。 –

+0

@Nandakishore:両方。UIImageは(高さ×幅×4)バイトのメモリを占有します。明らかに、高さと幅を小さくするとメモリの占有面積に大きな影響を与えます。 UIViewControllerのContentMode設定を使用すると、大きなイメージを小さく見せることができますが、UIImage自体を実際に縮小すると、メモリが小さくなります。 –

+0

しかし、画像を縮小することは、ダウンロードをバックグラウンドで行うための代替手段ではありません。それもまた起こる必要があります。 'initWithContentsOfUrl:'呼び出しが実行されている間、あなたのUIはフリーズしています。 –

関連する問題