UITableViewCell.imageView提供内存警告?

时间:2014-03-30 18:33:00

标签: ios objective-c cocoa-touch uitableview

我有一个表格视图控制器,我在每个单元格的图像视图中显示应用程序中记录的视频的缩略图。他们有90.5的高度,真的没什么特别的。但是,加载此视图会为内存增加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)];
}

1 个答案:

答案 0 :(得分:1)

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

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

你只需要具有较低的分辨率,它适用于细胞图像,高分辨率会像霰弹枪一样将空洞吹入记忆中。