Iphone 将图像制作成圆形
我一直在试图把一个UIImage遮罩成一个圆圈。 我现在使用的代码在其他答案中很流行,但尽管我得到了一个圆,但它的边缘非常参差不齐且不平滑。 有人能帮忙吗?我能得到一个完美光滑的圆吗 我用来创建遮罩的代码是:Iphone 将图像制作成圆形,iphone,objective-c,xcode,uiimageview,uiimage,Iphone,Objective C,Xcode,Uiimageview,Uiimage,我一直在试图把一个UIImage遮罩成一个圆圈。 我现在使用的代码在其他答案中很流行,但尽管我得到了一个圆,但它的边缘非常参差不齐且不平滑。 有人能帮忙吗?我能得到一个完美光滑的圆吗 我用来创建遮罩的代码是: (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage { CGImageRef imageNoAlpha = image.CGImage; CGColorSpaceRef cs = C
(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;
提前感谢…这可能会帮助您,将拐角半径作为要显示图像的参考视图的一半,或者您可以设置自定义矩形 例如,在我的例子中,我想在的“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;
}
试试这个代码
yourImageView.layer.cornerRadius = yourImageView.frame.size.height /2;
yourImageView.layer.masksToBounds = YES;
yourImageView.layer.borderWidth = 0;
此显示的图像与ios 7 circle图像类似,非常感谢对不起,格式太难看了。
阿托兹答案的更好版本
@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
如果您使用的是
UIImageView
,并且希望在保持圆角的同时设置图像大小的动画,则可以按如下方式应用变换:
目标-C
迅捷的
注意:这也将保留任何自动布局
约束
aToz和fnc12答案的更好版本(Swift):
用法:
self.userImageView.image = image.roundedImage()
我使用UIBezierPath解决问题
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:yourImageView.bounds];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = path.CGPath;
yourImageView.layer.mask = maskLayer;
从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;
}
如果您使用的是Swift,则可以导入“AlamofireImage”,并使用其功能之一。对于圆形图像:
let roundImage = UIImage(named: "myImage")!.af_imageRoundedIntoCircle()
试试这个
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];
注意:确保高度和宽度与该视图相同。感觉我应该把帽子扔进戒指里。高效、紧凑的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()
}
}
可能是。这两个链接可能有助于你寻找什么。1.2.谢谢我见过很多人说,你可以只使用角半径属性,但那是错误的。虽然它确实会使角变圆,但它不会使所有图像看起来像一个圆。仅供参考,此解决方案会使表格视图中的滚动变慢@aToz的解决方案工作得很好。它在tableView滚动中工作得很好,我看不出有任何问题,我的tableView包含246行,每行从服务器加载图像,并且仍然工作perfectly@aLFaRSi您正在运行什么类型的设备?性能差的代码并不总是在一开始就表现出来。这两种方法可能都有理论证明或基准。@Alvarsi。你很幸运;)这使用的是
UIImageView
,而主题启动程序要求一个带掩码的圆圈UIImage
。UIGraphicsBeginImageContextWithOptions中的第三个参数必须是0.0,而不是1.0。因为1.0非常吸引人ugly@fnc12非常感谢。我想知道为什么结果模糊难看。我们可以对非正方形图像进行裁剪和圆化吗?不推荐使用CGPointZero,请改用:CGRect(原点:.zero,大小:imageSize)。
+ (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;
}
let roundImage = UIImage(named: "myImage")!.af_imageRoundedIntoCircle()
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];
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()
}
}