본문 바로가기

옛글/아이폰 프로그래밍

iOS에서 나인패치(배경리사이즈) 적용하기

반응형

안드로이드에서는 나인패치가 없는 줄 알았는데,  (안드로이드에서는 작은 배경을 직선으로 늘려서 이미지가 깨지지 않고 적용) UIImage 에도 비슷한 메서드가 존재하네요. 


+(UIImage*)ninePatchedImage:(UIImage*)kOriginalImg Offset:(float)kOffset{

    CGSize kSize = kOriginalImg.size;

    return  [kOriginalImg resizableImageWithCapInsets:UIEdgeInsetsMake(aOffset, aOffset, kSize.height-aOffset, kSize.width-aOffset)];

}


resizableImageWithCapInsets 
[ iOS Document Reference]


resizableImageWithCapInsets:

Creates and returns a new image object with the specified cap insets.

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets

Parameters

capInsets

The values to use for the cap insets.

Return Value

A new image object with the specified cap insets.

Discussion

You use this method to add cap insets to an image or to change the existing cap insets of an image. In both cases, you get back a new image and the original image remains untouched.

During scaling or resizing of the image, areas covered by a cap are not scaled or resized. Instead, the pixel area not covered by the cap in each direction is tiled, left-to-right and top-to-bottom, to resize the image. This technique is often used to create variable-width buttons, which retain the same rounded corners but whose center region grows or shrinks as needed. For best performance, use a tiled area that is a 1x1 pixel area in size.

Availability

  • Available in iOS 5.0 and later.

Declared In

UIImage.h

반응형