如何擦除 UIImage 的一部分

How erase part of UIImage(如何擦除 UIImage 的一部分)
本文介绍了如何擦除 UIImage 的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法消除该图像的黑色背景

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

我在这个 topic 上找到了这个示例代码,但我不明白它是如何工作的

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.

问候,
kl94

Regards,
kl94

推荐答案

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

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.

这篇关于如何擦除 UIImage 的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How to stop UIBarButtonItem text from truncating?(如何阻止UIBarButtonItem文本被截断?)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
How to send an image with custom keyboard iOS(如何使用自定义键盘IO发送映像)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)