Skip to content

Commit

Permalink
Added subclass for NSImage-backed TUIImage. This class makes using PD…
Browse files Browse the repository at this point in the history
…F-based (and thus scalable) images easier and also helps for seamless support of retina display image resources (those "@2x" images).
  • Loading branch information
adib committed Aug 7, 2012
1 parent 8d0bb0b commit 6c3490c
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
12 changes: 12 additions & 0 deletions TwUI.xcodeproj/project.pbxproj
Expand Up @@ -122,6 +122,10 @@
88EFFB5413F417E200CF91A9 /* TUITextViewEditor.m in Sources */ = {isa = PBXBuildFile; fileRef = 88EFFB5013F417E200CF91A9 /* TUITextViewEditor.m */; };
88EFFB5513F417E200CF91A9 /* TUITextViewEditor.m in Sources */ = {isa = PBXBuildFile; fileRef = 88EFFB5013F417E200CF91A9 /* TUITextViewEditor.m */; };
88EFFB5613F417E200CF91A9 /* TUITextViewEditor.m in Sources */ = {isa = PBXBuildFile; fileRef = 88EFFB5013F417E200CF91A9 /* TUITextViewEditor.m */; };
C09019E215D0D5C10088A39C /* TUINSImage.h in Headers */ = {isa = PBXBuildFile; fileRef = C09019E015D0D5C10088A39C /* TUINSImage.h */; };
C09019E315D0D5C10088A39C /* TUINSImage.m in Sources */ = {isa = PBXBuildFile; fileRef = C09019E115D0D5C10088A39C /* TUINSImage.m */; };
C09019E515D0DD2B0088A39C /* TUINSImage.m in Sources */ = {isa = PBXBuildFile; fileRef = C09019E115D0D5C10088A39C /* TUINSImage.m */; };
C09019E615D0DD2C0088A39C /* TUINSImage.m in Sources */ = {isa = PBXBuildFile; fileRef = C09019E115D0D5C10088A39C /* TUINSImage.m */; };
CB5B265A13BE6DA200579B1E /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = CB5B265813BE6DA200579B1E /* InfoPlist.strings */; };
CB5B266313BE6DA300579B1E /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CB5B266213BE6DA300579B1E /* SenTestingKit.framework */; };
CB5B266413BE6DA300579B1E /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CB5B264F13BE6DA200579B1E /* Cocoa.framework */; };
Expand Down Expand Up @@ -302,6 +306,8 @@
88D25F5413F5D96500CFAAA9 /* TUITableView+Cell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "TUITableView+Cell.m"; sourceTree = "<group>"; };
88EFFB4F13F417E200CF91A9 /* TUITextViewEditor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TUITextViewEditor.h; sourceTree = "<group>"; };
88EFFB5013F417E200CF91A9 /* TUITextViewEditor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TUITextViewEditor.m; sourceTree = "<group>"; };
C09019E015D0D5C10088A39C /* TUINSImage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TUINSImage.h; sourceTree = "<group>"; };
C09019E115D0D5C10088A39C /* TUINSImage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TUINSImage.m; sourceTree = "<group>"; };
CB5B264C13BE6DA200579B1E /* TwUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TwUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
CB5B264F13BE6DA200579B1E /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
CB5B265213BE6DA200579B1E /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -661,6 +667,8 @@
CBB74C8E13BE6E1900C85CB5 /* TUIViewController.m */,
CBB74C8F13BE6E1900C85CB5 /* TUIViewNSViewContainer.h */,
CBB74C9013BE6E1900C85CB5 /* TUIViewNSViewContainer.m */,
C09019E015D0D5C10088A39C /* TUINSImage.h */,
C09019E115D0D5C10088A39C /* TUINSImage.m */,
);
name = UIKit;
path = lib/UIKit;
Expand Down Expand Up @@ -742,6 +750,7 @@
88EFFB5113F417E200CF91A9 /* TUITextViewEditor.h in Headers */,
88D25F5513F5D96500CFAAA9 /* TUITableView+Cell.h in Headers */,
88A4AFDE145A16CA0071CF22 /* TUITextRenderer+Accessibility.h in Headers */,
C09019E215D0D5C10088A39C /* TUINSImage.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -961,6 +970,7 @@
887F273113F9969800D75DE6 /* TUITableViewSectionHeader.m in Sources */,
884E8F5715387E11000F7A8D /* TUIPopover.m in Sources */,
884E8F601538809C000F7A8D /* CAAnimation+TUIExtensions.m in Sources */,
C09019E615D0DD2C0088A39C /* TUINSImage.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1025,6 +1035,7 @@
88A4AFDF145A16CA0071CF22 /* TUITextRenderer+Accessibility.m in Sources */,
884E8F5515387E11000F7A8D /* TUIPopover.m in Sources */,
884E8F5E1538809C000F7A8D /* CAAnimation+TUIExtensions.m in Sources */,
C09019E315D0D5C10088A39C /* TUINSImage.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1097,6 +1108,7 @@
887F273013F9969800D75DE6 /* TUITableViewSectionHeader.m in Sources */,
884E8F5615387E11000F7A8D /* TUIPopover.m in Sources */,
884E8F5F1538809C000F7A8D /* CAAnimation+TUIExtensions.m in Sources */,
C09019E515D0DD2B0088A39C /* TUINSImage.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
25 changes: 25 additions & 0 deletions lib/UIKit/TUINSImage.h
@@ -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
121 changes: 121 additions & 0 deletions lib/UIKit/TUINSImage.m
@@ -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

0 comments on commit 6c3490c

Please sign in to comment.