如何擦除UIImage的一部分 [英] How erase part of UIImage
问题描述
有没有方法可以删除该图像的黑色背景
Is there a way to erase the black background for that image
我在这个主题上找到了这个示例代码,但我没有了解它是如何工作的
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
推荐答案
代码正在使用石英(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的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!