2014-03-30 29 views
0

我有一個表格視圖控制器,其中我在每個單元格的imageview中顯示應用程序中記錄的視頻的縮略圖。他們有90.5的高度,並沒有什麼特別的。然而,加載這個視圖會增加40 MB到內存,這太多了。我不知道爲什麼會發生這種情況,有人可以提供一些見解嗎?UITableViewCell.imageView給內存警告?

知道縮略圖作爲二進制數據存儲在視頻對象中可能很有用。

- (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

,如果這是該行造成內存警告:

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

你只需要擁有較低的分辨率,它是一個細胞圖像,高分辨率會像霰彈槍一樣將孔洞打入記憶。

+0

嗯。這是一個高清視頻的快照,但縮小到130x90,然後在0.1的JPGRepresentation,所以它不應該很大..? –

+0

你當時是正確的。我忘了將130x90分配爲新的縮略圖。哎呦。感謝,並與霰彈槍很好的比喻。 –