Permalink
Browse files

Made TUIImage more friendly to potential subclasses.

  • Loading branch information...
1 parent c95dce2 commit 8d0bb0b40325b3a510d0afe38ea125c69706f025 @adib committed Aug 7, 2012
Showing with 20 additions and 16 deletions.
  1. +7 −7 lib/UIKit/TUIImage+Drawing.m
  2. +13 −9 lib/UIKit/TUIImage.m
@@ -33,7 +33,7 @@ + (TUIImage *)imageWithSize:(CGSize)size drawing:(void(^)(CGContextRef))draw
- (TUIImage *)scale:(CGSize)size
{
- return [TUIImage imageWithSize:size drawing:^(CGContextRef ctx) {
+ return [[self class] imageWithSize:size drawing:^(CGContextRef ctx) {
CGRect r;
r.origin = CGPointZero;
r.size = size;
@@ -56,12 +56,12 @@ - (TUIImage *)crop:(CGRect)cropRect
NSLog(@"CGImageCreateWithImageInRect failed %@ %@", NSStringFromRect(cropRect), NSStringFromSize(s));
return nil;
}
- TUIImage *i = [TUIImage imageWithCGImage:cgimage];
+ TUIImage *i = [[self class] imageWithCGImage:cgimage];
CGImageRelease(cgimage);
return i;
} else {
// slow crop - probably doing pad
- return [TUIImage imageWithSize:cropRect.size drawing:^(CGContextRef ctx) {
+ return [[self class] imageWithSize:cropRect.size drawing:^(CGContextRef ctx) {
CGRect imageRect;
imageRect.origin.x = -cropRect.origin.x;
imageRect.origin.y = -cropRect.origin.y;
@@ -106,7 +106,7 @@ - (TUIImage *)roundImage:(CGFloat)radius
CGRect r;
r.origin = CGPointZero;
r.size = self.size;
- return [TUIImage imageWithSize:r.size drawing:^(CGContextRef ctx) {
+ return [[self class] imageWithSize:r.size drawing:^(CGContextRef ctx) {
CGContextClipToRoundRect(ctx, r, radius);
CGContextDrawImage(ctx, r, self.CGImage);
}];
@@ -115,7 +115,7 @@ - (TUIImage *)roundImage:(CGFloat)radius
- (TUIImage *)invertedMask
{
CGSize s = self.size;
- return [TUIImage imageWithSize:s drawing:^(CGContextRef ctx) {
+ return [[self class] imageWithSize:s drawing:^(CGContextRef ctx) {
CGRect rect = CGRectMake(0, 0, s.width, s.height);
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
CGContextFillRect(ctx, rect);
@@ -130,7 +130,7 @@ - (TUIImage *)innerShadowWithOffset:(CGSize)offset radius:(CGFloat)radius color:
{
CGFloat padding = ceil(radius);
TUIImage *paddedImage = [self pad:padding];
- TUIImage *shadowImage = [TUIImage imageWithSize:paddedImage.size drawing:^(CGContextRef ctx) {
+ TUIImage *shadowImage = [[self class] imageWithSize:paddedImage.size drawing:^(CGContextRef ctx) {
CGContextSaveGState(ctx);
CGRect r = CGRectMake(0, 0, paddedImage.size.width, paddedImage.size.height);
CGContextClipToMask(ctx, r, paddedImage.CGImage); // clip to image
@@ -153,7 +153,7 @@ - (TUIImage *)embossMaskWithOffset:(CGSize)offset
CGFloat padding = MAX(offset.width, offset.height) + 1;
TUIImage *paddedImage = [self pad:padding];
CGSize s = paddedImage.size;
- TUIImage *embossedImage = [TUIImage imageWithSize:s drawing:^(CGContextRef ctx) {
+ TUIImage *embossedImage = [[self class] imageWithSize:s drawing:^(CGContextRef ctx) {
CGContextSaveGState(ctx);
CGRect r = CGRectMake(0, 0, s.width, s.height);
CGContextClipToMask(ctx, r, [paddedImage CGImage]);
View
@@ -87,7 +87,7 @@ + (TUIImage *)imageWithData:(NSData *)data
return nil;
}
- TUIImage *i = [TUIImage imageWithCGImage:image];
+ TUIImage *i = [self imageWithCGImage:image];
CGImageRelease(image);
CFRelease(imageSource);
return i;
@@ -133,7 +133,7 @@ + (TUIImage *)imageWithNSImage:(NSImage *)image
for(NSImageRep *rep in [image representations]){
CGImageRef cgImage;
if([rep isKindOfClass:[NSBitmapImageRep class]] && (cgImage = [(NSBitmapImageRep *)rep CGImage]) != nil){
- return [[TUIImage alloc] initWithCGImage:cgImage];
+ return [[self alloc] initWithCGImage:cgImage];
}
}
#endif
@@ -178,7 +178,8 @@ + (TUIImage *)imageWithNSImage:(NSImage *)image
- (CGSize)size
{
- return CGSizeMake(CGImageGetWidth(_imageRef), CGImageGetHeight(_imageRef));
+ CGImageRef imageRef = self.CGImage;
+ return CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
}
- (CGImageRef)CGImage
@@ -206,12 +207,13 @@ - (void)drawInRect:(CGRect)rect
- (void)drawInRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha
{
- if(_imageRef) {
+ CGImageRef imageRef = self.CGImage;
+ if(imageRef) {
CGContextRef ctx = TUIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextSetAlpha(ctx, alpha);
CGContextSetBlendMode(ctx, blendMode);
- CGContextDrawImage(ctx, rect, _imageRef);
+ CGContextDrawImage(ctx, rect, imageRef);
CGContextRestoreGState(ctx);
}
}
@@ -228,20 +230,21 @@ - (NSInteger)topCapHeight
- (TUIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
{
- TUIStretchableImage *i = (TUIStretchableImage *)[TUIStretchableImage imageWithCGImage:_imageRef];
+ TUIStretchableImage *i = (TUIStretchableImage *)[TUIStretchableImage imageWithCGImage:self.CGImage];
i->leftCapWidth = leftCapWidth;
i->topCapHeight = topCapHeight;
return i;
}
- (NSData *)dataRepresentationForType:(NSString *)type compression:(CGFloat)compressionQuality
{
- if(_imageRef) {
+ CGImageRef imageRef = self.CGImage;
+ if(imageRef) {
NSMutableData *mutableData = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)mutableData, (__bridge CFStringRef)type, 1, NULL);
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:compressionQuality], kCGImageDestinationLossyCompressionQuality, nil];
- CGImageDestinationAddImage(destination, _imageRef, (__bridge CFDictionaryRef)properties);
+ CGImageDestinationAddImage(destination, imageRef, (__bridge CFDictionaryRef)properties);
CGImageDestinationFinalize(destination);
CFRelease(destination);
@@ -321,7 +324,8 @@ - (void)drawInRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)
if(t*2 > s.height-1) t -= 1;
if(l*2 > s.width-1) l -= 1;
- if(_imageRef) {
+ CGImageRef imageRef = self.CGImage;
+ if(imageRef) {
if(!_flags.haveSlices) {
STRETCH_COORDS(0.0, 0.0, s.width, s.height, t, l, t, l)
#define X(I) slices[I] = [self upsideDownCrop:r[I]];

0 comments on commit 8d0bb0b

Please sign in to comment.