How erase part of 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 的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何擦除 UIImage 的一部分
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01