如何删除UIImage的一部分
有没有办法删除该图像的黑色背景
我找到了此示例代码 主题,但我不明白它是如何工作的,
- (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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该代码使用quartz(iPhone 的图形引擎)来剪辑图像。一些细节:
您首先需要某种“目标”来绘制。在图形中,这通常称为上下文。在上面,您告诉系统需要一个给定大小的(位图)图像上下文,然后您获得对它的引用。
在这里,您提供上下文的路径,然后剪辑上下文。也就是说“只在这些区域进行绘制”。
您在新的上下文中绘制图像。仅绘制剪切区域,因此其余部分被有效擦除。
现在,您只需要求系统返回您在上面设置的上下文(目标)中绘制的图像即可。
注意:这是一个粗略的描述,您可能想查看每个函数的参考以获取更多详细信息。
The code is using quartz (iPhone's graphics engine) to clip the image. some details:
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.
Here, you provide a path to the context, and then clip the context. That's to say "only draw in these regions."
You draw the image in the new context. Only the clipped region will be drawn, so the rest is effectively erased.
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.
以下是如何获取“erasePath”。只需将要裁剪的所选 CGRect 传递为“cropRect”即可 -
Here's how to get "erasePath". Just pass the selected CGRect you want to crop as "cropRect" -