Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a UIImage to a circle form

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...

like image 271
Shahar Nechmad Avatar asked Sep 13 '11 09:09

Shahar Nechmad


People also ask

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .


1 Answers

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

like image 103
Waseem Shah Avatar answered Sep 19 '22 12:09

Waseem Shah