ios 带圆角的 UIImage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10563986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIImage with rounded corners
提问by iWheelBuy
I have read several posts already. And almost everyone suggest using QuartzCore/QuartzCore.h
with layer.cornerRadius
我已经阅读了几篇文章。而且几乎每个人都建议使用QuartzCore/QuartzCore.h
withlayer.cornerRadius
I have tried this method and some more methods.
我已经尝试过这种方法和其他一些方法。
Well, everything works fine when an image doesn't move.
好吧,当图像不动时,一切正常。
But in my project I always move my images. If I add corner radius or a shadow the image movement is no longer smooth. And it looks aweful!
但在我的项目中,我总是移动我的图像。如果我添加角半径或阴影,图像移动不再平滑。它看起来棒极了!
That is the way I resize my image:
这就是我调整图像大小的方式:
CGSize newSize = CGSizeMake(200*height/image.size.height, 280);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil, newSize.width, newSize.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextClearRect(context, CGRectMake(0, 0, newSize.width, newSize.height));
CGContextDrawImage(context, CGRectMake(0, 0, newSize.width, newSize.height), image.CGImage);
CGImageRef scaledImage = CGBitmapContextCreateImage(context);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
UIImage *newImage = [UIImage imageWithCGImage: scaledImage];
CGImageRelease(scaledImage);
I would like to know a good way to resize my image, add shadow, corner radius and still have smooth movement of my image.
我想知道一种调整图像大小、添加阴影、角半径并且仍然使图像平滑移动的好方法。
回答by MCKapur
// Get your image somehow
UIImage *image = [UIImage imageNamed:@"image.jpg"];
// Begin a new image that will be the new image with the rounded corners
// (here with the size of an UIImageView)
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
cornerRadius:10.0] addClip];
// Draw your image
[image drawInRect:imageView.bounds];
// Get the image, here setting the UIImageView image
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
Try moving with this code, as far as I can remember I used this a while back that makes an image with rounded corners that you can move around into the targets. But it seemed to scale fine...
尝试使用此代码移动,据我所知,我不久前使用过它来制作带有圆角的图像,您可以将其移动到目标中。但它似乎可以很好地扩展......
回答by Charlie Seligman
Rohan's answer is a great one. If anyone is having any probs refactoring it to work in their projects here is a neat refactor that might hopefully help some newer programmers:
罗汉的回答很好。如果有人在重构它以在他们的项目中工作时遇到任何问题,这里是一个整洁的重构,希望能帮助一些新程序员:
+ (UIImage *)imageWithRoundedCornersSize:(float)cornerRadius usingImage:(UIImage *)original
{
UIImageView *imageView = [[UIImageView alloc] initWithImage:original];
// Begin a new image that will be the new image with the rounded corners
// (here with the size of an UIImageView)
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
cornerRadius:cornerRadius] addClip];
// Draw your image
[original drawInRect:imageView.bounds];
// Get the image, here setting the UIImageView image
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
return imageView.image;
}
回答by alexmorhun
There is more optimized memory version of solution of Charlie Seligman. You do not need use UIImageView for this purpose.
还有就是查理·塞利格曼的解决方案更优化的内存版本。为此,您不需要使用 UIImageView。
+ (UIImage *)imageWithRoundedCornersSize:(float)cornerRadius usingImage:(UIImage *)original
{
CGRect frame = CGRectMake(0, 0, original.size.width, original.size.height);
// Begin a new image that will be the new image with the rounded corners
// (here with the size of an UIImageView)
UIGraphicsBeginImageContextWithOptions(original.size, NO, original.scale);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:frame
cornerRadius:cornerRadius] addClip];
// Draw your image
[original drawInRect:frame];
// Get the image, here setting the UIImageView image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
return image;
}
You can get the round image by specifying cornerRadius equal increased image size twice (image.size.width * 2).
您可以通过将cornerRadius 指定为两倍增加的图像大小(image.size.width * 2)来获得圆形图像。
回答by Paras Joshi
try bellow code after import the file #import in your .m file
在 .m 文件中导入文件 #import 后尝试以下代码
yourImageView.layer.shadowColor=[[UIColor grayColor] CGColor];
yourImageView.layer.cornerRadius = 8;
yourImageView.layer.shadowOffset=CGSizeMake(2, 2);
yourImageView.layer.shadowOpacity=1.0;
yourImageView.layer.shadowRadius=1.0;
Hope ,this help you....
希望,这对你有帮助......
回答by Javier Soto
If perfomance is bad when animating the image position, it's probably because it's having to render the image with the corner radius every frame. This is inevitable if behind the image there's not a solid color (the "blending" is different in each frame, and thus has to be calculated). If this is not the case, and you can get the background to be of a solid color, make sure you set the UIView.backgroundColor
to something other than clearColor, with an alpha 1.0.
Also rasterizing the layer might help you (it will store the a cached version of the rendered layer in memory and use that throughout the animation. But again, won't help if the layer is not opaque).
如果动画图像位置时,性能比较差,可能是因为它不必渲染与圆角半径每帧的图像。如果图像后面没有纯色,这是不可避免的(每帧中的“混合”都不同,因此必须进行计算)。如果不是这种情况,并且您可以将背景设为纯色,请确保将 设置为UIView.backgroundColor
clearColor 以外的其他内容,并使用 alpha 1.0。栅格化图层也可能对您有所帮助(它会将渲染图层的缓存版本存储在内存中并在整个动画中使用它。但同样,如果图层不透明,则无济于事)。
This is how you do this:
这是你如何做到这一点:
UIView *yourView;
CALayer *yourLayer = yourView.layer;
yourLayer.shouldRasterize = YES;
yourLayer.rasterizationScale = [UIScreen mainScreen].scale;
回答by Kiran Bhoraniya
Set rounded corners on an image in Objective-C:
在 Objective-C 中为图像设置圆角:
yourImageView.layer.cornerRadius = yourRadius;
yourImageView.clipsToBounds = YES;
回答by Dixit Patel
Yes, it is possible.
对的,这是可能的。
Import the QuartzCore (#import ) header and play with the layer property of the UIImageView.
导入 QuartzCore (#import) 标头并使用 UIImageView 的 layer 属性。
ImageView.layer.cornerRadius = Radius;
ImageView.clipsToBounds = YES;