티스토리 뷰

Mobile/iOS

[iOS]UIImage radius넣어주기

out of coding 2016. 4. 4. 23:59

UIImage를 UIImageView에 넣어주고 Layer를 조정하여서 화면에 보여주는 부분을 둥글게 표현을 할 수 있습니다.

그렇지만, 이런것을 떠나서 UIImage 자체를 둥글게 표현을 하고 싶을 경우가 있을수도 있는데요.

이러한 부분은 다음과 같은 코딩으로 처리할수 있음.


구현을 UIImage에 Category를 사용하여서, 만들어주도록 합니다.


@implementation UIImage (Add)


- (UIImage*)cornerRadius:(const CGFloat)radius {

    UIImage* image = self;

    

    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);

    

    const CGRect RECT = CGRectMake(0, 0, image.size.width, image.size.height);

    

    [[UIBezierPath bezierPathWithRoundedRect:RECT cornerRadius:radius] addClip];

    

    [image drawInRect:RECT];

    

    UIImage* imageNew = UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();

    

    return imageNew;

}


@end


호출부는 다음과 같음.


UIImage* image = [[UIImage imageNamed:@"image.jpeg"] cornerRadius:10];


카테고리에 대한 설명은 하지 않겠습니다. ^^

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함