forked from twitter-archive/twui
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added subclass for NSImage-backed TUIImage. This class makes using PD…
…F-based (and thus scalable) images easier and also helps for seamless support of retina display image resources (those "@2x" images).
- Loading branch information
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// TUINSImage.h | ||
// TwUI | ||
// | ||
// A TUIImage subclass backed by an NSImage as it's first-priority data store instead of a CGImageRef | ||
// Intended for better support of multi-resolution images (i.e. PDF or @2x image resource files for Retina Display) | ||
// Created by Sasmito Adibowo on 07-08-12. | ||
// | ||
// | ||
|
||
#import "TUIImage.h" | ||
|
||
@interface TUINSImage : TUIImage | ||
|
||
- (TUINSImage *)initWithNSImageNoCopy:(NSImage *)image; | ||
+ (TUINSImage *)imageWithNSImage:(NSImage *)image; | ||
+ (TUINSImage *)imageNamed:(NSString *)name cache:(BOOL)shouldCache; | ||
+ (TUINSImage *)imageWithData:(NSData *)data; | ||
|
||
@end | ||
|
||
|
||
@interface TUINSImage (AppKit) | ||
@property (nonatomic, readonly) id nsImage; // NSImage * | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// | ||
// TUINSImage.m | ||
// TwUI | ||
// | ||
// Created by Sasmito Adibowo on 07-08-12. | ||
// | ||
// | ||
|
||
#import "TUINSImage.h" | ||
|
||
|
||
#if !__has_feature(objc_arc) | ||
#error Need automatic reference counting to compile this. | ||
#endif | ||
|
||
|
||
@implementation TUINSImage { | ||
NSImage* _backingImage; | ||
} | ||
|
||
-(void)dealloc | ||
{ | ||
|
||
} | ||
|
||
- (TUINSImage *)initWithNSImageNoCopy:(NSImage *)image | ||
{ | ||
if ((self = [super init])) { | ||
_backingImage = image; | ||
} | ||
return self; | ||
} | ||
|
||
|
||
- (id)initWithCGImage:(CGImageRef)imageRef | ||
{ | ||
if((self = [super initWithCGImage:imageRef])) { | ||
if (imageRef) { | ||
_backingImage = [[NSImage alloc] initWithCGImage:imageRef size:NSZeroSize]; | ||
} | ||
} | ||
return self; | ||
} | ||
|
||
|
||
|
||
+ (TUINSImage *)imageWithNSImage:(NSImage *)image | ||
{ | ||
return [[self alloc] initWithNSImageNoCopy:[image copy]]; | ||
} | ||
|
||
|
||
+ (TUINSImage *)imageNamed:(NSString *)name cache:(BOOL)shouldCache | ||
{ | ||
if(!name) | ||
return nil; | ||
|
||
static NSMutableDictionary *cache = nil; | ||
if(!cache && shouldCache) { | ||
cache = [[NSMutableDictionary alloc] init]; | ||
} | ||
|
||
TUINSImage *image = [cache objectForKey:name]; | ||
if(image) { | ||
return image; | ||
} | ||
|
||
NSImage* backingImage = [NSImage imageNamed:name]; | ||
if (backingImage) { | ||
image = [[self alloc] initWithNSImageNoCopy:backingImage]; | ||
if (shouldCache) { | ||
[cache setObject:image forKey:name]; | ||
} | ||
} | ||
|
||
return image; | ||
} | ||
|
||
|
||
+ (TUINSImage *)imageWithData:(NSData *)data | ||
{ | ||
NSImage* backingImage = [[NSImage alloc] initWithData:data]; | ||
if (backingImage) { | ||
return [[[self class] alloc] initWithNSImageNoCopy:backingImage]; | ||
} | ||
return nil; | ||
} | ||
|
||
|
||
- (CGSize)size | ||
{ | ||
return [_backingImage size]; | ||
} | ||
|
||
|
||
- (CGImageRef)CGImage | ||
{ | ||
if(!_imageRef) { | ||
_imageRef = [_backingImage CGImageForProposedRect:NULL context:NULL hints:nil]; | ||
if(_imageRef) { | ||
CGImageRetain(_imageRef); | ||
} | ||
} | ||
return _imageRef; | ||
} | ||
|
||
|
||
@end | ||
|
||
|
||
|
||
@implementation TUINSImage (AppKit) | ||
|
||
- (id)nsImage | ||
{ | ||
// follow the convention of TUIImage that creates a new instance of NSImage. | ||
return [_backingImage copy]; | ||
} | ||
|
||
@end | ||
|