简体   繁体   English

UIImage修剪圆圈IOS周围的白色区域

[英]UIImage trim white areas around circle IOS

As the title says I have many images which show a circle. 如标题所示,我有很多显示圆圈的图像。 The problem is that the image is a square, and I would like to make transparent the white areas around the circle. 问题是图像是正方形,我想使圆周围的白色区域透明。 This is one of the images: 这是图像之一:

在此处输入图片说明

Unfortunately the white areas are not visible because the background of stack overflow is white. 不幸的是,白色区域不可见,因为堆栈溢出的背景是白色的。 Is there the possibility to remove the four white corners (without removing all white areas as some of the elements in the circle could be white)? 是否可以删除四个白色角(不删除所有白色区域,因为圆圈中的某些元素可能是白色的)?

In photoshop I would use the "magic wand" tool, if you know what I mean. 如果您知道我的意思,在photoshop中,我将使用“魔术棒”工具。 Thanks for any help. 谢谢你的帮助。

Simply set the corner radius to half the image width or height (assuming the images are square, of course): 只需将拐角半径设置为图像宽度或高度的一半即可(当然,假设图像是正方形的):

#import <QuartzCore/QuartzCore.h>

imageView.layer.cornerRadius  = imageView.bounds.size.width;
imageView.layer.masksToBounds = YES;

And, if you like an (additional) border: 而且,如果您喜欢(其他)边框:

imageView.layer.borderWidth = 0.5f;

Then, reading your comment, to get an image of this: 然后,阅读您的评论,以获取此图像:

UIGraphicsBeginImageContext(imageView.layer.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

The rendered image is exactly the size of the layer now. 现在,渲染的image正好是图层的大小。

To prevent pixelation when scaling up, try this: 为了防止在放大时像素化,请尝试以下操作:

BOOL  opaque = NO;
short scale  = [[UIScreen mainScreen] scale];
UIGraphicsBeginImageContextWithOptions(view.bounds.size, opaque, scale);

or perhaps with another scale. 或其他规模。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM