【问题标题】:Large Images Crashing UITableView app大图像崩溃 UITableView 应用程序
【发布时间】:2013-09-20 00:32:23
【问题描述】:

所以我一直在寻找这个问题的答案,但仍然无法弄清楚。我有一个包含 15 个图像的数组,所以我试图在 UITableViewCell 中使用子视图进行显示。下面的代码 - 我读过的所有内容都提到使用自动释放/释放来解决问题,但我在尝试这样做时只会遇到 ARC 错误。任何帮助将不胜感激。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    int countthis = indexPath.row;
    NSString *image = imgarr[countthis];
    UIImageView *iv = [[UIImageView alloc] initWithFrame:(CGRect){.size={320, tableView.rowHeight}}];
    iv.image = [UIImage imageNamed:image];
    cell.imageView.image = iv.image;
    return cell;
}

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    无论您的域是什么,大文件都会引起问题。具体来说,Apple 表示:

    您应该避免创建尺寸大于 1024 x 1024 的 UIImage 对象。

    您似乎正在尝试调整图像大小,但 UIImages 是不可变的。所以你分配的 UIImageView 只会浪费处理器周期。

    如果您遇到需要按比例缩小的大图像,请在将它们分配给单元格之前考虑进行缩放。您可能会发现这些例程很有用:The simplest way to resize an UIImage?

    重新自动释放/释放:自 ARC 以来已弃用。您的代码似乎没有泄漏内存。我不会出汗的。但是您应该编辑您的问题以包含有关崩溃的详细信息。

    【讨论】:

      【解决方案2】:

      您的代码可以对此进行清理,这可能对性能有所帮助。您不需要将indexPath.row 转换为int,因为它已经是NSInteger,它是一种依赖于体系结构的类型(int 表示32 位,long 表示64 位)。您可能还想使用self.imgarr,因为它可能是您班级中的一个属性。图像变化如尼尔所说。

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *CellIdentifier = @"Cell";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil) {
              cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
          }
      
          NSString *image = self.imgarr[indexPath.row];
          cell.imageView.image = [UIImage imageNamed:image];
          return cell;
      }
      

      至于自动发布/发布,您提到使用它们时遇到 ARC 错误,这表明您使用的是 iOS 5 或更高版本的 SDK。您的代码中不再需要它们。

      【讨论】:

        【解决方案3】:

        在表格视图中显示图像之前,您可以尝试使用CGImageSourceCreateThumbnailAtIndexfirst 调整图像大小。

        如果你有想要调整大小的图片的路径,你可以使用这个:

        - (void)resizeImageAtPath:(NSString *)imagePath {
            // Create the image source (from path)
            CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) [NSURL fileURLWithPath:imagePath], NULL);
        
            // To create image source from UIImage, use this
            // NSData* pngData =  UIImagePNGRepresentation(image);
            // CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)pngData, NULL);
        
            // Create thumbnail options
            CFDictionaryRef options = (__bridge CFDictionaryRef) @{
                    (id) kCGImageSourceCreateThumbnailWithTransform : @YES,
                    (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
                    (id) kCGImageSourceThumbnailMaxPixelSize : @(640)
            };
            // Generate the thumbnail
            CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options); 
            CFRelease(src);
            // Write the thumbnail at path
            CGImageWriteToFile(thumbnail, imagePath);
        }
        

        更多详情here.

        【讨论】:

          猜你喜欢
          • 2012-08-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-29
          • 2012-12-07
          • 1970-01-01
          • 2019-02-13
          • 1970-01-01
          相关资源
          最近更新 更多