Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resizableImageWithCapInsets for NSImage?

In UIKit we have - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets.

Is there something similar in AppKit for creating a tileable NSImage?

like image 496
lmirosevic Avatar asked Feb 04 '13 14:02

lmirosevic


3 Answers

NSImage did get a slight improvement in 10.10 (Yosemite). NSImage now has the property:

@property NSEdgeInsets capInsets

With this property, you can set the cap insets just like you would in iOS. If you call [image drawInRect:rect], it will take these insets into account. Be aware that this will only work on systems that are running 10.10 or higher; on older systems it will simply stretch the image.

like image 68
Lucas Derraugh Avatar answered Nov 10 '22 11:11

Lucas Derraugh


Nope. NSImage contains no such smarts. You will have to chop up, resize, and reassemble the image yourself.

You might look into making an NSCustomImageRep subclass that implements this, which you could then use to implement an OS X version of the same method.

like image 45
Peter Hosey Avatar answered Nov 10 '22 10:11

Peter Hosey


You can use [NSImage drawAtPoint] to instead, like this:

@implementation NSImage (CapInsets)

- (void)drawImageWithLeftCapWidth:(NSInteger)left topCapHeight:(NSInteger)top destRect:(NSRect)dstRect
{
    NSSize imgSize = self.size;
    [self drawAtPoint:dstRect.origin fromRect:NSMakeRect(0, 0, left, top) operation:NSCompositeSourceOver fraction:1];
    [self drawInRect:NSMakeRect(left, 0, dstRect.size.width-2*left, top) fromRect:NSMakeRect(left, 0, imgSize.width-2*left, top) operation:NSCompositeSourceOver fraction:1];
    [self drawAtPoint:NSMakePoint(dstRect.origin.x+dstRect.size.width-left, dstRect.origin.y) fromRect:NSMakeRect(imgSize.width-left, 0, left, top) operation:NSCompositeSourceOver fraction:1]; 
}

@end
like image 4
fengxing Avatar answered Nov 10 '22 11:11

fengxing