2011-02-03 52 views
0

嗨,我正在使用Twitter,我得到的圖片有問題,我有推文視圖有UITableview,它包含與用戶照片的所有推文,如果我加載這些照片在每個單元格當我滾動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

請編輯您的帖子突出的代碼。這是不可能的。 – 2011-02-03 12:15:34

回答

1

表視圖滾動緩慢的原因是您正試圖獲取主線程上每個單元格的圖像數據。當UI將從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用於後臺加載。 -1爲NSOperationQueue和它的朋友,當ASIHTTPRequest將做更多的工作,這麼少的工作。淨0. ;-) – 2011-02-03 13:13:21

0

This article(對於無緒表下載圖片),可能有你的答案。 iOS開發庫中還有this sample,它顯示瞭如何儘可能快地異步加載圖像。

0

確實要按照其他回答者的描述懶惰和異步下載圖片。

您也可能希望將其調整爲縮略圖大小並將其存儲在內存中的較小尺寸。有關教程和實際可用的庫代碼,請參閱this article(通過複製和粘貼開發向下滾動)。

編輯:你顯然需要更多的幫助與背景下載部分。這是我如何做到的。

安裝ASIHTTPRequest,這是一個大大簡化HTTP客戶端工作的第三方庫。按照說明將其安裝到您的項目中,並確保您將#include "ASIHTTPRequest.h"放在.m文件的頂部。

然後在你的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,是縮略圖使用,以減少圖片的內存大小或只是大圖片的大小,以小圖片大小,謝謝 – 2011-02-03 14:00:16