【问题标题】:Memory Issue when loading images in UITableviewcell?在 UITableviewcell 中加载图像时出现内存问题?
【发布时间】:2014-03-25 12:09:37
【问题描述】:

我正在 UItableViewCell 中从服务器加载图像。

由于每个图像需要 10MB 大小,因此会导致内存问题。 每当我滚动到tableView 时,应用程序就会崩溃

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    locationcellObject=[self.tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *temp= [sortedArray objectAtIndex:indexPath.row];
    locationcellObject.title.text=[temp objectForKey:@"locationtitle"];
    locationcellObject.subtitle_Lbl.text=[temp objectForKey:@"category"];
    NSString *trimmedtitle = [[temp objectForKey:@"locationtitle"]stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *name=[NSString stringWithFormat:@"images/%@.png",trimmedtitle];
    NSString *imageName=[NSString stringWithFormat:@"http://my_URL_HERE/%@",name];
    _tempData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]];
    UIImage *display=[[UIImage alloc]initWithData:_tempData];
    locationcellObject.locationPic_img_View.image=display;
    locationcellObject.locationPic_img_View.contentMode=UIViewContentModeScaleAspectFit;

    return locationcellObject;
}

有什么简单的方法吗??

【问题讨论】:

  • 你在哪里分配你的单元格?
  • 为什么要为单元格使用 10MB 的图像?较小尺寸的图像就足够了,因为它可能无论如何都会被缩放(除非您的单元格大小与图像一样大)
  • 我为Cell 创建了一个NSObject,并在ViewDidLoad 中分配了它
  • @giorashc 是的,我的单元格将占据屏幕的一半

标签: ios objective-c uitableview uiimage


【解决方案1】:

在后台线程中下载您的图像,在块中下载图像。通过这种方式,两个线程将在您的应用程序主线程和后台线程中运行。这将减少主线程的负载。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    //Call your function or whatever work that needs to be done
    //Code in this part is run on a background thread

    dispatch_async(dispatch_get_main_queue(), ^(void) {

        //Stop your activity indicator or anything else with the GUI
        //Code here is run on the main thread

    });
}); 

并且还使用 NSCache 将下载的图像添加到缓存中,下次您的图像将从缓存中加载,

你可以查看link这里你可以找到如何在缓存中添加图片

【讨论】:

    【解决方案2】:

    请参考THIS 教程。它以高效的方式描述了在 UItableView 中获取/加载图像。

    【讨论】:

      【解决方案3】:

      1 ) 你应该在后台进行下载
      2 ) 制作图片的缩略图(图片尺寸较小)

       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^ {
      _tempData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]];
      UIImage *display=[[UIImage alloc]initWithData:_tempData];
      
      //make a thumbnail of the image 
      
      UIImage *display; 
      CGSize destinationSize = ...;
      UIGraphicsBeginImageContext(destinationSize);
      [originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      //
      dispatch_async(dispatch_get_main_queue(), ^{
      //put result to main thread
      locationcellObject.locationPic_img_View.image= newImage;
           });
      });
      

      【讨论】:

      • main_queue 图像未显示在UIImageViewCell
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多