Create UIImage cutout(创建 UIImage 抠图)
问题描述
我有一个 UIView
包含 2 个 UIImageViews
- 一个框架和框架后面的图片.框架不是矩形 - 它是不规则的形状.用户可以操作框架后面的图片(缩放、旋转和平移),完成后,我想捕捉框架内图片的剪切 - 而不是图片和框架一起.有什么办法可以做到吗?
I have a UIView
containing 2 UIImageViews
- a frame and a picture behind the frame. The frame is not a rectangle - it's an irregular shape. The user can manipulate the picture behind the frame (zoom, rotate and pan) and when they're done, I want to capture the cutout of the picture within the frame - not the picture and the frame together. Is there a way I can do this?
我已经设法将图片和框架拼合为一个图像,如下所示,但我只想要图片,如果成功提取,它将具有框架形状的边框.
I've managed to flatten the picture and the frame together in to a single image as below, but I only want the picture, which if extracted successfully will have a border in the shape of the frame.
- (IBAction)renderPhoto:(id)sender {
//Combine the layers into a single image
UIView *canvas = [[[sender superview] subviews] objectAtIndex:0];
UIGraphicsBeginImageContext(canvas.bounds.size);
[canvas.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
推荐答案
你可以从你的框架中创建一个蒙版,并将这个蒙版应用到目标图像上,以从目标图像上切下框架.或者,根据您最终使用最终图像的方式,在执行绘图时使用框架剪辑上下文可能更简单.
You could create a mask from your frame and apply this mask to the target image to essentially cut the frame off the target image. Or, depending how you end up using the final image, it may be simpler to clip the context using the frame while you perform your drawing.
本主题涵盖了所有内容:https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_images/dq_images.html#//apple_ref/doc/uid/TP30001066-CH212-TPXREF101
This topic covers it all: https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_images/dq_images.html#//apple_ref/doc/uid/TP30001066-CH212-TPXREF101
第二个选项(剪切上下文)位于 通过剪切上下文来屏蔽图像.
The second option (clipping the context) is under Masking an Image by Clipping the Context.
这篇关于创建 UIImage 抠图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:创建 UIImage 抠图
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01