ios - UITableViewCell.imageView 给出内存警告?

标签 ios objective-c cocoa-touch uitableview

我有一个 TableView Controller ,在该 Controller 中,我在每个单元格的 ImageView 中显示应用程序中录制的视频的缩略图。他们的高度都是90.5,确实没什么特别的。但是,加载此 View 会增加 40 MB 内存,这太多了。我不确定为什么会这样,有人可以提供一些见解吗?

了解缩略图作为二进制数据存储在视频对象中可能会很有用。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Video Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    self.tableView.rowHeight = 90.5;
    cell.imageView.tag = indexPath.row;


    Video *video = [self.videoArray objectAtIndex:indexPath.row];

    cell.textLabel.text = [self.dateFormatter stringFromDate:video.createdAt];

    cell.detailTextLabel.text = video.video_description;

    cell.detailTextLabel.text = @"Tap on the thumbnail to add video information!";

    cell.imageView.image = [UIImage imageWithData:video.thumbnail];

    UIView *highlighted = [[UIView alloc]initWithFrame:cell.bounds];

    [highlighted setBackgroundColor:[UIColor colorWithRed:0 green:122.0 / 255.0 blue:1.0 alpha:1.0]];

    cell.selectedBackgroundView = highlighted;
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];
    cell.detailTextLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.70];

    return cell;
}

这是我获取缩略图的方法:

- (UIImage *)generateThumbnailAtURL:(NSURL *)URL {

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:URL options:nil];
    AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    //generate.appliesPreferredTrackTransform = YES;
    NSError *err = nil;
    double halfTime = ((CMTimeGetSeconds(asset.duration)) / 2);
    CMTime time = CMTimeMake(halfTime, 1);
    CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:nil error:&err];
    UIImage *thumbnail = [[UIImage alloc] initWithCGImage:imgRef];
    CGImageRelease(imgRef);

    [thumbnail imageByScalingToSize:CGSizeMake(130,90) contentMode:UIViewContentModeScaleAspectFill];

    return [UIImage imageWithData:UIImageJPEGRepresentation(thumbnail, 0.1)];
}

最佳答案

如果这是导致内存警告的行:

cell.imageView.image = [UIImage imageWithData:video.thumbnail];

你只需要有较低的分辨率,它用于细胞图像,高分辨率会像霰弹枪一样在内存中打洞。

关于ios - UITableViewCell.imageView 给出内存警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22748049/

相关文章:

ios - 使用reverseGeocodeLocation:设置地址字符串,然后从方法返回

iphone - NSMutableOrderedSet和NSNull行为?

iphone - 如何根据 UILabel 中的数字显示文本

ios - 具有倍增效果的 Swift UIView

ios - 如何使用 objective-C 在 TableView 中突出显示多行

ios - NSCaseInsensitiveSearch RangeOfString 搜索 Swift

ios - UITableView 不响应 UITableViewCell 的选择

ios - NSPredicate ANY 多列

ios - 如何将渐变背景颜色添加到 Storyboard中的原型(prototype) UITableViewCell?

ios - 在自定义 tableviewcell 中更改 UIImage