-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIImage+RoundedImage.m
More file actions
37 lines (27 loc) · 1.12 KB
/
UIImage+RoundedImage.m
File metadata and controls
37 lines (27 loc) · 1.12 KB
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
29
30
31
32
33
34
35
36
37
//
// UIImage+RoundedImage.m
// GreenPaw
//
// Created by Nikolay Dyankov on 7/10/13.
// Copyright (c) 2013 Nikolay Dyankov. All rights reserved.
//
#import "UIImage+RoundedImage.h"
@implementation UIImage (RoundedImage)
+ (UIImage *)roundedImageWithImage:(UIImage *)image {
if (image) {
CGContextRef cx = CGBitmapContextCreate(NULL, image.size.width, image.size.height, CGImageGetBitsPerComponent(image.CGImage), 0, CGImageGetColorSpace(image.CGImage), CGImageGetBitmapInfo(image.CGImage));
CGContextBeginPath(cx);
CGRect pathRect = CGRectMake(0, 0, image.size.width, image.size.height);
CGContextAddEllipseInRect(cx, pathRect);
CGContextClosePath(cx);
CGContextClip(cx);
CGContextDrawImage(cx, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
CGImageRef clippedImage = CGBitmapContextCreateImage(cx);
CGContextRelease(cx);
UIImage *roundedImage = [UIImage imageWithCGImage:clippedImage];
CGImageRelease(clippedImage);
return roundedImage;
}
return nil;
}
@end