From 792e8a595b395eb98e9ce8f9bd0dd1304644e42f Mon Sep 17 00:00:00 2001 From: ritsuka Date: Tue, 12 May 2015 13:14:29 +0000 Subject: MacGui: limit the preview images cache to ~ the size of 60 1080p images. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@7171 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- macosx/HBPreviewGenerator.m | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/macosx/HBPreviewGenerator.m b/macosx/HBPreviewGenerator.m index 345c574c8..e1cf20e26 100644 --- a/macosx/HBPreviewGenerator.m +++ b/macosx/HBPreviewGenerator.m @@ -15,7 +15,7 @@ @interface HBPreviewGenerator () -@property (nonatomic, readonly) NSMutableDictionary *picturePreviews; +@property (nonatomic, readonly) NSCache *picturePreviews; @property (unsafe_unretained, nonatomic, readonly) HBCore *scanCore; @property (unsafe_unretained, nonatomic, readonly) HBJob *job; @@ -32,7 +32,11 @@ { _scanCore = core; _job = job; - _picturePreviews = [[NSMutableDictionary alloc] init]; + + _picturePreviews = [[NSCache alloc] init]; + // Limit the cache to 60 1080p previews, the cost is in pixels + _picturePreviews.totalCostLimit = 60 * 1920 * 1080; + _imagesCount = [[[NSUserDefaults standardUserDefaults] objectForKey:@"PreviewsNumber"] intValue]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imagesSettingsDidChange) name:HBPictureChangedNotification object:job.picture]; @@ -62,7 +66,7 @@ // The preview for the specified index may not currently exist, so this method // generates it if necessary. - CGImageRef theImage = (__bridge CGImageRef)(self.picturePreviews)[@(index)]; + CGImageRef theImage = (__bridge CGImageRef)([self.picturePreviews objectForKey:@(index)]); if (!theImage) { @@ -75,7 +79,9 @@ deinterlace:deinterlace]; if (cache && theImage) { - (self.picturePreviews)[@(index)] = (__bridge id)theImage; + // The cost is the number of pixels of the image + NSUInteger previewCost = CGImageGetWidth(theImage) * CGImageGetHeight(theImage); + [self.picturePreviews setObject:(__bridge id)(theImage) forKey:@(index) cost:previewCost]; } } else -- cgit v1.2.3