如何获取 UIImage 并给它一个黑色边框?
如何设置 UIImage 的边框?
How can I set the border of a UIImage
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何设置 UIImage 的边框?
How can I set the border of a UIImage
?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(16)
您可以为 UIImageView 添加边框,然后根据图像大小更改 UIimageView 的大小:
确保将 imageView 的原点设置为中心
You can add border to the UIImageView, and then change the size of the UIimageView according to the image size:
Make sure you to set the origin of the imageView to the center
您可以操纵图像本身,但更好的方法是简单地添加包含 UIImageView 的 UIView,并将背景更改为黑色。然后将该容器视图的大小设置为比 UIImageView 稍大一些。
You could manipulate the image itself, but a much better way is to simply add a UIView that contains the UIImageView, and change the background to black. Then set the size of that container view to a little bit larger than the UIImageView.
另一种方法是直接从设计师那里做。
选择您的图像并进入“显示身份检查器”。
您可以在此处手动添加“用户定义的运行时属性”:
Another way is to do directly from designer.
Select your image and go under "Show the Identity inspector".
Here you can manually add "User Defined Runtime Attributes":
对于那些在 UIImage 上寻找即插即用解决方案的人,我写了 CodyMace 的答案作为扩展。
用法:
let displayed = UIImage(named: "something")?.outline()
For those looking for a plug-and-play solution on UIImage, I wrote CodyMace's answer as an extension.
Usage:
let outlined = UIImage(named: "something")?.outline()
//你需要导入
&然后对于边框中的 ImageView
//you need to import
& then for ImageView in border
此功能将返回带有黑色边框的图像尝试这个..希望这会对您有所帮助
This function will return you image with black border try this.. hope this will help you
在 Swift 3 中,您可以按照以下方式对 UIImage 本身进行操作:
In Swift 3 here's how you do it to the UIImage itself:
我创建了一个类,为 imageView h 添加边框。使用此类而不是 UIImageView。我已经给出了 4 的填充。您可以根据您的意愿给出。
I have created a class which adds a border to imageView h. Use this class instead of UIImageView.I have given a padding of 4. You can give as per your wish.
我使用此方法在图像外部添加边框。您可以在
boderWidth
常量中自定义边框宽度。雨燕3
I use this method to add a border outside the image. You can customise the border width in
boderWidth
constant.Swift 3
与操作系统> 3.0你可以这样做:
With OS > 3.0 you can do this:
您可以通过创建一个新图像来完成此操作(也在该问题的其他帖子中得到回答):
此代码将在图像周围产生粉红色边框。但是,如果您只想显示边框,则使用 UIImageView 的图层并设置其边框。
You can do this by creating a new image (also answered in your other posting of this question):
This code will produce a pink border around the image. However if you are going to just display the border then use the layer of the
UIImageView
and set its border.此代码可用于添加
UIImageView
视图边框。This code can be used for adding
UIImageView
view border.如果您知道图像的尺寸,那么向 UIImageView 的图层添加边框是最好的解决方案。事实上,您可以简单地将 imageView 设置为 x,y,image.size.width,image.size.height
如果您有一个固定大小的 ImageView,其中动态加载的图像正在调整大小(或缩放到 AspectFit),那么您的目标是将图像视图的大小调整为新的调整大小的图像。
最短的方法是:
但是要使用 AVMakeRectWithAspectRatioInsideRect,您需要将此
导入语句添加到您的文件中,并在您的项目中包含 AVFoundation 框架(与 SDK 捆绑在一起)。
If you know the dimensions of your image, then adding a border to the UIImageView's layer is the best solution AFAIK. Infact, you can simply setFrame your imageView to x,y,image.size.width,image.size.height
In case you have an ImageView of a fixed size with dynamically loaded images which are getting resized (or scaled to AspectFit), then your aim is to resize the imageview to the new resized image.
The shortest way to do this:
But to use the AVMakeRectWithAspectRatioInsideRect, you need to add this
import statement to your file and also include the AVFoundation framework in your project (comes bundled with the SDK).
您无法添加边框,但这可以达到相同的效果。您还可以将本例中名为 blackBG 的 UIView 制作为具有边框图像和空白中间的 UIImageView,然后您将拥有自定义图像边框,而不仅仅是黑色。
You can't add a border, but this would work for the same effect. You could also make the UIView called blackBG in this example into a UIImageView with a border image and a blank middle, and then you'd have a custom image border instead of just black.
所有这些答案都工作正常,但向图像添加一个矩形。
假设您有一个形状(在我的例子中是蝴蝶)并且您想添加边框(红色边框):
我们需要两个步骤:
1) 获取图像,转换为 CGImage,传递给函数以使用 CoreGraphics 在上下文中在屏幕外绘制,并返回一个新的 CGImage
2) 转换为 uiimage 返回并绘制:
}
}
我添加了一个普通的蝴蝶和一个红色边框更大的蝴蝶。
all these answers work fine BUT add a rect to an image.
Suppose You have a shape (in my case a butterfly) and You want to add a border (a red border):
we need two steps:
1) take the image, convert to CGImage, pass to a function to draw offscreen in a context using CoreGraphics, and give back a new CGImage
2) convert to uiimage back and draw:
}
}
I added a normal butterfly and a red-bordered bigger butterfly.