2011-02-03 54 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