如何删除UIImage的一部分

发布于 2024-10-08 22:04:07 字数 647 浏览 6 评论 0原文

有没有办法删除该图像的黑色背景 alt text

我找到了此示例代码 主题,但我不明白它是如何工作的,

- (void)clipImage {
  UIImage *img = imgView.image;
  CGSize s = img.size;
  UIGraphicsBeginImageContext(s);
  CGContextRef g = UIGraphicsGetCurrentContext();
  CGContextAddPath(g,erasePath);
  CGContextAddRect(g,CGRectMake(0,0,s.width,s.height));
  CGContextEOClip(g);
  [img drawAtPoint:CGPointZero];
  imageView.image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
}

也许我的方式是错误的。

问候,
KL94

Is there a way to erase the black background for that image
alt text

I've found this sample code found on this topic but i don't understand how it works

- (void)clipImage {
  UIImage *img = imgView.image;
  CGSize s = img.size;
  UIGraphicsBeginImageContext(s);
  CGContextRef g = UIGraphicsGetCurrentContext();
  CGContextAddPath(g,erasePath);
  CGContextAddRect(g,CGRectMake(0,0,s.width,s.height));
  CGContextEOClip(g);
  [img drawAtPoint:CGPointZero];
  imageView.image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
}

Maybe I'm in the wrong way.

Regards,
kl94

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

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

发布评论

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

评论(2

年华零落成诗 2024-10-15 22:04:07

该代码使用quartz(iPhone 的图形引擎)来剪辑图像。一些细节:

UIGraphicsBeginImageContext(s);
CGContextRef g = UIGraphicsGetCurrentContext();

您首先需要某种“目标”来绘制。在图形中,这通常称为上下文。在上面,您告诉系统需要一个给定大小的(位图)图像上下文,然后您获得对它的引用。

CGContextAddPath(g,erasePath);
CGContextAddRect(g,CGRectMake(0,0,s.width,s.height));
CGContextEOClip(g);

在这里,您提供上下文的路径,然后剪辑上下文。也就是说“只在这些区域进行绘制”。

[img drawAtPoint:CGPointZero];

您在新的上下文中绘制图像。仅绘制剪切区域,因此其余部分被有效擦除。

imageView.image = UIGraphicsGetImageFromCurrentImageContext();

现在,您只需要求系统返回您在上面设置的上下文(目标)中绘制的图像即可。

注意:这是一个粗略的描述,您可能想查看每个函数的参考以获取更多详细信息。

The code is using quartz (iPhone's graphics engine) to clip the image. some details:

UIGraphicsBeginImageContext(s);
CGContextRef g = UIGraphicsGetCurrentContext();

You first need some sort of "target" to draw to. In graphics, that's usually called a context. Above, you tell the system a (bitmap) image context with given size is needed and then you get a reference to it.

CGContextAddPath(g,erasePath);
CGContextAddRect(g,CGRectMake(0,0,s.width,s.height));
CGContextEOClip(g);

Here, you provide a path to the context, and then clip the context. That's to say "only draw in these regions."

[img drawAtPoint:CGPointZero];

You draw the image in the new context. Only the clipped region will be drawn, so the rest is effectively erased.

imageView.image = UIGraphicsGetImageFromCurrentImageContext();

Now here, you just ask the system to give you back the image drawn in the context (target) you set up above

Note: this is a rough description, you might wanna look at the reference for each function to get more details.

无力看清 2024-10-15 22:04:07

以下是如何获取“erasePath”。只需将要裁剪的所选 CGRect 传递为“cropRect”即可 -

CGMutablePathRef erasePath = CGPathCreateMutable();
CGPathAddRect(erasePath, NULL, cropRect);
CGContextAddPath(g,erasePath);

Here's how to get "erasePath". Just pass the selected CGRect you want to crop as "cropRect" -

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