Reputation: 391
I have been trying to mask a UIImage into a circle. I'm using now the code that has been popular on other answers here, but although I do get a circle its edges are very jagged and not smooth. Anyone can help? I do I get a perfect, smooth circle?
The code I'm using to create the mask is:
(UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef imageNoAlpha = image.CGImage;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGFloat width = CGImageGetWidth(imageNoAlpha);
CGFloat height = CGImageGetWidth(imageNoAlpha);
CGContextRef ctxWithAlpha = CGBitmapContextCreate(nil, width, height, 8, 4*width, cs, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(ctxWithAlpha, CGRectMake(0, 0, width, height), imageNoAlpha);
CGImageRef imageWithAlpha = CGBitmapContextCreateImage(ctxWithAlpha);
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
UIImage* retImage= [UIImage imageWithCGImage:masked];
UIImage* retImageFixed = [retImage transparentBorderImage:1];
CGImageRelease(mask);
CGImageRelease(masked);
CGImageRelease(imageWithAlpha);
CGContextRelease(ctxWithAlpha);
CGColorSpaceRelease(cs);
return retImageFixed;
Thanks in advance...
Upvotes: 38
Views: 24937
Reputation: 2237
Sorry for ugly formatting. Better version of aToz's answer.
@interface UIImage (CircleMask)
+ (UIImage *)roundedRectImageFromImage :(UIImage *)image
size :(CGSize)imageSize
withCornerRadius :(float)cornerRadius;
@end
@implementation UIImage(CircleMask)
+(UIImage*)roundedRectImageFromImage:(UIImage *)image
size:(CGSize)imageSize
withCornerRadius:(float)cornerRadius
{
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0); // <= notice 0.0 as third scale parameter. It is important cause default draw scale ≠ 1.0. Try 1.0 - it will draw an ugly image..
CGRect bounds = (CGRect){CGPointZero,imageSize};
[[UIBezierPath bezierPathWithRoundedRect:bounds
cornerRadius:cornerRadius] addClip];
[image drawInRect:bounds];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
@end
The same thing in Swift:
extension UIImage{
class func roundedRectImageFromImage(image:UIImage,imageSize:CGSize,cornerRadius:CGFloat)->UIImage{
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0.0)
let bounds = CGRect(origin: CGPointZero, size: imageSize)
UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).addClip()
image.drawInRect(bounds)
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return finalImage
}
}
Upvotes: 17
Reputation: 670
Felt like I should throw my hat into the ring. An effective, compact Swift 5 solution:
extension UIImage {
var rounded: UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
defer { UIGraphicsEndImageContext() }
let bounds = CGRect(origin: .zero, size: size)
UIBezierPath(roundedRect: bounds, cornerRadius: size.height/2.0).addClip()
draw(in: bounds)
return UIGraphicsGetImageFromCurrentImageContext()
}
}
Upvotes: 3
Reputation: 20
try this
UIImageView *circleImageview=[[UIImageView alloc]init];
circleImageview.frame=CGRectMake(0,10, 80, 80)];
circleImageview.layer.cornerRadius=circleimagview.frame.size.height/2;
circleImageview.userInteractionEnabled=YES;
[self.view addSubview:circleImageview];
Note:Make Sure height and width will be same of that view.
Upvotes: -1
Reputation: 4751
If you're using Swift, you can import 'AlamofireImage', and use one of its funcs. For circled image:
let roundImage = UIImage(named: "myImage")!.af_imageRoundedIntoCircle()
Upvotes: 1
Reputation: 5743
What about getting help from UIImageView
+ (UIImage *)circularImage:(UIImage *)image withDiameter:(NSUInteger)diameter
{
CGRect frame = CGRectMake(0.0f, 0.0f, diameter, diameter);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
imageView.image = image;
CALayer *layer = imageView.layer;
layer.masksToBounds = YES;
layer.cornerRadius =MAX( imageView.frame.size.height,imageView.frame.size.width)/2;
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size,NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
[layer renderInContext:context];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return roundedImage;
}
Upvotes: 1
Reputation: 19
I resolve my problem with UIBezierPath
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:yourImageView.bounds];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = path.CGPath;
yourImageView.layer.mask = maskLayer;
Upvotes: -1
Reputation: 9590
A better version of aToz's and fnc12's answers (in Swift):
extension UIImage
{
func roundImage() -> UIImage
{
let newImage = self.copy() as! UIImage
let cornerRadius = self.size.height/2
UIGraphicsBeginImageContextWithOptions(self.size, false, 1.0)
let bounds = CGRect(origin: CGPointZero, size: self.size)
UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).addClip()
newImage.drawInRect(bounds)
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return finalImage
}
}
Usage:
self.userImageView.image = image.roundedImage()
Upvotes: 15
Reputation: 1724
If you are using an UIImageView
and you are looking to animate the size of the image while maintaining the rounded corners, you can apply a transformation as follows:
Objective-C
// Our UIImageView reference
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
- (void)viewDidLoad {
[super viewDidLoad];
myImageView.layer.cornerRadius = myImageView.frame.size.width / 2.0f;
myImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4f, 0.4f);
}
- (void)viewDidAppear:(BOOL)animated {
[UIView animateWithDuration:1.0 animations:^{
myImageView.transform = CGAffineTransformIdentity;
}];
}
Swift
// Our UIImageView reference
@IBOutlet weak var myImageView:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
myImageView.layer.cornerRadius = myImageView.frame.size.width / 2;
myImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4)
}
override func viewDidAppear(animated: Bool) {
UIView.animateWithDuration(1.0) {
myImageView.transform = CGAffineTransformIdentity;
}
}
NOTE: This will also maintain any AutoLayout constraints
.
Upvotes: 0
Reputation: 2219
try this code
yourImageView.layer.cornerRadius = yourImageView.frame.size.height /2;
yourImageView.layer.masksToBounds = YES;
yourImageView.layer.borderWidth = 0;
this show image like ios 7 circle image thanks
Upvotes: 50
Reputation: 2464
This might help you, pass corner radius as half of the reference view where you want to display the image or you can set a custom rect,
eg. In my case I want to display the image on a "imageView", so the corner radius in this case would be imageView.frame.size.width/2
- (void)displayImage
{
imageView.image = [self getRoundedRectImageFromImage:@"Test.png" onReferenceView:imageView withCornerRadius: imageView.frame.size.width/2];
}
- (UIImage *)getRoundedRectImageFromImage :(UIImage *)image onReferenceView :(UIImageView*)imageView withCornerRadius :(float)cornerRadius
{
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
cornerRadius:cornerRadius] addClip];
[image drawInRect:imageView.bounds];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
Upvotes: 28