我如何以编程方式获取 UIImage 并给它一个黑色边框?

发布于 2024-08-03 21:48:37 字数 73 浏览 1 评论 0原文

我如何以编程方式获取 UIImage 并给它一个黑色边框?

如果我能收到代码,那就太好了。

tnx

How can i take an UIImage and give it a black border programmatically?

If i can receive code, it will be great.

tnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿透光 2024-08-10 21:48:37

如果您只需要显示边框,则可以使用 UIImageView 层上的核心动画来实现。如果您需要在图像本身上执行此操作,那么您将需要创建一个新图像,将旧图像绘制到新图像中,然后在其顶部绘制一个矩形。

- (UIImage*)imageWithBorderFromImage:(UIImage*)source;
{
  CGSize size = [source size];
  UIGraphicsBeginImageContext(size);
  CGRect rect = CGRectMake(0, 0, size.width, size.height);
  [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];

  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetRGBStrokeColor(context, 1.0, 0.5, 1.0, 1.0); 
  CGContextStrokeRect(context, rect);
  UIImage *testImg =  UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return testImg;
}  

这将在图像上添加粉红色边框并返回新图像。

If you only need to display the border you can do that with Core Animation on the UIImageView's layer. If you need to do it on the image itself then you will need to create a new image, draw the old image into the new image and then draw a rect on top of it.

- (UIImage*)imageWithBorderFromImage:(UIImage*)source;
{
  CGSize size = [source size];
  UIGraphicsBeginImageContext(size);
  CGRect rect = CGRectMake(0, 0, size.width, size.height);
  [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];

  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetRGBStrokeColor(context, 1.0, 0.5, 1.0, 1.0); 
  CGContextStrokeRect(context, rect);
  UIImage *testImg =  UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return testImg;
}  

This will put a pink border on an image and return the new image.

吹泡泡o 2024-08-10 21:48:37

我想看看这个:
我可以编辑 UIImage 属性 CGImage 的像素

至于黑色边框部分,我想你应该能猜出来。只需沿着每条边迭代并将像素更改为 (0,0,0,255) 一定数量。

I'd have a look at this:
Can I Edit the Pixels of the UIImage's Property CGImage

As for the black border part, I assume you can figure that one out. Just iterate along each side and change the pixels to (0,0,0,255) for a certain amount.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文