Reputation: 5089
I have a table view controller in which I display thumbnails in each cell's imageview for videos recorded within the app. They have a 90.5 height, and really nothing special. However, loading this view adds 40 mb to memory, which is way too much. I'm not sure why this is happening, can someone give some insight?
It might be useful to know that the thumbnails are stored in the video object as binary data.
- (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;
}
Here is how I get the thumbnail:
- (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)];
}
Upvotes: 0
Views: 313
Reputation: 45490
if this is the line causing memory warning:
cell.imageView.image = [UIImage imageWithData:video.thumbnail];
You just need to have lower resolution, it is for an cell image, high resolution will blow holes into memory like a shotgun.
Upvotes: 1