왼쪽, 가운데, 오른쪽 분할이미지로 하나의 조합된 이미지를 생성하는 방법입니다.
왼쪽/오른쪽 이미지는 고정이고, 가운데를 늘리는 방식으로 처리합니다.
이런식으로 이미지 리소스를 디자인하는 곳이 너무 많군요..
별로 좋은것 같지 않은데...쓸데없이 이미지만 많아지고,,,
+ (NSImage *) createImage:(NSBundle *)bundle withFrame:(NSRect)frame withSliceImages:(NSArray *)imageArr
NSImage *leftImage, *middleImage, *rightImage;
filePath = [bundle pathForResource:[imageArr objectAtIndex:0] ofType:nil];
leftImage = [[NSImage alloc] initWithContentsOfFile:filePath];
[leftImage setSize:NSMakeSize([leftImage size].width, frame.size.height )];
[leftImage setScalesWhenResized:YES];
filePath = [bundle pathForResource:[imageArr objectAtIndex:1] ofType:nil];
middleImage = [[NSImage alloc] initWithContentsOfFile:filePath];
filePath = [bundle pathForResource:[imageArr objectAtIndex:2] ofType:nil];
rightImage = [[NSImage alloc] initWithContentsOfFile:filePath];
[rightImage setSize:NSMakeSize([rightImage size].width, frame.size.height )];
[rightImage setScalesWhenResized:YES];
NSInteger leftWidth = [leftImage size].width;
NSInteger rightWidth = [rightImage size].width;
[middleImage setSize:NSMakeSize(frame.size.width - (leftWidth + rightWidth), frame.size.height)];
[middleImage setScalesWhenResized:YES];
NSInteger midWidth = [middleImage size].width;
NSImage *newImage =[[NSImage alloc] initWithSize:NSMakeSize(leftWidth + rightWidth + midWidth, frame.size.height)];
[leftImage compositeToPoint:NSMakePoint(0, 0) operation:NSCompositeSourceOver];
[middleImage compositeToPoint:NSMakePoint(leftWidth, 0) operation:NSCompositeSourceOver];
[rightImage compositeToPoint:NSMakePoint(leftWidth+midWidth, 0) operation:NSCompositeSourceOver];
참고로, NSCell.h 에는 아래와 같은 메소드가 존재한다.
NSCell 을 상속받아서 커스텀 셀을 구성할 때, 분할이미지를 사용해야하는 경우는
10.5 에서는 좌우캡이 반드시 있어야만 가운데 영역을 그릴수 있으므로, 주의한다.
/* Draw an image from two end caps and a fill. The end caps are scaled proportionally to match the thickness of the destination frame. In the horizontal case, the startCap is drawn into the left part of the destination, the endCap is drawn into the right part of the destination, and the fill is tiled over the remaining area. The caps and the fill should all be the same height. The vertical case is similar.
This is an appropriate way to draw the bezel of a button that can be resized in one dimension.
APPKIT_EXTERN void NSDrawThreePartImage(NSRect frame, NSImage *startCap, NSImage *centerFill, NSImage *endCap, BOOL vertical, NSCompositingOperation op, CGFloat alphaFraction, BOOL flipped) AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
/* Draw an image from nine pieces. When drawn, the destination rect is partitioned into nine rectangular regions: the corner pieces are the natural size of the corner images, the edge pieces are the natural size of the edge fill images in the direction perpendicular to the edge and flush with the corners. The center rect fills the remaining space. The supplied images and fills are drawn into the corresponding regions, with fill images tiled at their natural dimensions. Images that share a border should have the same thickness in that dimension.
This method is appropriate for the bezel of a control, like a box, that can be resized in both dimensions.
APPKIT_EXTERN void NSDrawNinePartImage(NSRect frame, NSImage *topLeftCorner, NSImage *topEdgeFill, NSImage *topRightCorner, NSImage *leftEdgeFill, NSImage *centerFill, NSImage *rightEdgeFill, NSImage *bottomLeftCorner, NSImage *bottomEdgeFill, NSImage *bottomRightCorner, NSCompositingOperation op, CGFloat alphaFraction, BOOL flipped) AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;