In UIKit
we have - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
.
Is there something similar in AppKit
for creating a tileable NSImage
?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With