我一直试图将UIImage掩盖成一个圆圈。 我现在正在使用在其他答案上流行的代码,但是虽然我得到一个圆圈,但它的边缘非常锯齿状而且不平滑。 有人可以帮忙吗?我能得到一个完美,流畅的圆圈吗?
我用来创建掩码的代码是:
(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;
提前致谢...
答案 0 :(得分:49)
试试这段代码
yourImageView.layer.cornerRadius = yourImageView.frame.size.height /2;
yourImageView.layer.masksToBounds = YES;
yourImageView.layer.borderWidth = 0;
这个显示图像像ios 7圈图像谢谢
答案 1 :(得分:25)
这可能对您有所帮助,将角半径作为参考视图的一半传递到您想要显示图像的位置,或者您可以设置自定义矩形,
例如。在我的情况下,我想在“imageView”上显示图像,因此在这种情况下,角半径将是 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;
}
答案 2 :(得分:15)
抱歉丑陋的格式化。 更好的版本aToz的答案。
@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
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
}
}
答案 3 :(得分:12)
更好的版本的aToz和fnc12的答案(在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
}
}
用法:
self.userImageView.image = image.roundedImage()
答案 4 :(得分:1)
如何从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;
}
答案 5 :(得分:0)
如果您使用UIImageView
并且想要在保持圆角的同时设置图像大小的动画,则可以按如下方式应用转换:
目标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;
}];
}
夫特
// 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;
}
}
注意:这也会保留任何自动布局 constraints
。
答案 6 :(得分:0)
如果您正在使用Swift,则可以导入“AlamofireImage”,并使用其中一个func。对于带圆圈的图像:
let roundImage = UIImage(named: "myImage")!.af_imageRoundedIntoCircle()
答案 7 :(得分:0)
试试这个
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];
注意:确保高度和宽度与该视图相同。
答案 8 :(得分:0)
感觉就像我应该把帽子扔进戒指。有效,紧凑的 Swift 5 解决方案:
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()
}
}
答案 9 :(得分:-1)
我使用 UIBezierPath
解决了我的问题UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:yourImageView.bounds];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = path.CGPath;
yourImageView.layer.mask = maskLayer;