创建UIImage剪切 [英] Create UIImage cutout

查看:91
本文介绍了创建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剪切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆