BathyScapheで画像のインラインプレビューを可能にするプラグイン
Revision | 80c6b52bc2ab171a491b8d7aeb5d62e5838efce3 (tree) |
---|---|
Time | 2012-05-18 23:16:01 |
Author | masakih <masakih@user...> |
Commiter | masakih |
[Mod] NSCacheを使用するようにした
merge from extended
@@ -45,3 +45,5 @@ | ||
45 | 45 | - (id)previewAttributes; |
46 | 46 | |
47 | 47 | @end |
48 | + | |
49 | +extern NSString *const BSInlinePreviewerPreviewed; |
@@ -29,7 +29,7 @@ | ||
29 | 29 | |
30 | 30 | @implementation BSInlinePreviewer |
31 | 31 | |
32 | -static NSString *const BSInlinePreviewerPreviewed = @"BSInlinePreviewerPreviewed"; | |
32 | +NSString *const BSInlinePreviewerPreviewed = @"BSInlinePreviewerPreviewed"; | |
33 | 33 | const NSUInteger alreadyPreviewed = NSNotFound - 1; |
34 | 34 | |
35 | 35 | @synthesize totalDownloads, remainder; |
@@ -43,6 +43,7 @@ const NSUInteger alreadyPreviewed = NSNotFound - 1; | ||
43 | 43 | [self setPreferences:prefs]; |
44 | 44 | cache = [[NSCache alloc] init]; |
45 | 45 | [cache setName:@"BSInlinePreviewer"]; |
46 | + [cache setCountLimit:100]; | |
46 | 47 | } |
47 | 48 | |
48 | 49 | return self; |
@@ -441,7 +442,7 @@ const NSUInteger alreadyPreviewed = NSNotFound - 1; | ||
441 | 442 | - (NSImage *)downloadImageURL:(NSURL *)imageURL |
442 | 443 | { |
443 | 444 | NSImage *cachedImage = [cache objectForKey:[self cacheKeyForURL:imageURL]]; |
444 | - if(cachedImage) return [self fitImage:cachedImage toSize:[self previewSize]];; | |
445 | + if(cachedImage) return cachedImage; | |
445 | 446 | |
446 | 447 | NSURLRequest *req; |
447 | 448 |
@@ -457,24 +458,27 @@ const NSUInteger alreadyPreviewed = NSNotFound - 1; | ||
457 | 458 | error:&err]; |
458 | 459 | if(err) { |
459 | 460 | NSLog(@"Fail download. reason(%@)", [err localizedDescription]); |
460 | - return [self notFoundImage]; | |
461 | + goto notFound; | |
461 | 462 | } |
462 | 463 | if(res) { |
463 | 464 | if(![[res MIMEType] hasPrefix:@"image/"]) { |
464 | 465 | NSLog(@"Fail download. reason(target type is %@)", [res MIMEType]); |
465 | - return [self notFoundImage]; | |
466 | + goto notFound; | |
466 | 467 | } |
467 | 468 | } |
468 | 469 | |
469 | 470 | NSImage *image = [[[NSImage alloc] initWithData:imageData] autorelease]; |
470 | 471 | if(!image) { |
471 | 472 | NSLog(@"Can not create image."); |
472 | - return [self notFoundImage]; | |
473 | + goto notFound; | |
473 | 474 | } |
474 | 475 | |
475 | - [cache setObject:image forKey:[self cacheKeyForURL:imageURL]]; | |
476 | - | |
477 | - return [self fitImage:image toSize:[self previewSize]]; | |
476 | + cachedImage = [self fitImage:image toSize:[self previewSize]]; | |
477 | + [cache setObject:cachedImage forKey:[self cacheKeyForURL:imageURL]]; | |
478 | + return cachedImage; | |
479 | +notFound: | |
480 | + [cache setObject:[self notFoundImage] forKey:[self cacheKeyForURL:imageURL]]; | |
481 | + return [self notFoundImage]; | |
478 | 482 | } |
479 | 483 | |
480 | 484 | @end |