如何只选择UIView的一部分转换为UIImage [英] How to select only a portion of UIView for converting to UIImage

查看:106
本文介绍了如何只选择UIView的一部分转换为UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下内容用于将UIView转换为UIImage

I have the following for converting a UIView to UIImage

+(UIImage*) createUIImageFromView:(UIView *)view frame:(CGRect)frame;
{
    if (UIGraphicsBeginImageContextWithOptions != NULL)
    {
        UIGraphicsBeginImageContextWithOptions(frame.size, NO, 2.0f);
    }
    else
    {
        UIGraphicsBeginImageContext(view.frame.size);
    }

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

但是,我只能选择图片的大小而不能选择图片的大小UIView应该转换为图像。例如,如果我只想转换矩形CGRect(10,10,20,20)而不是整个UIView,我该怎么做?

However, I can only select the size of the image but not where in the UIView should be converted to the image. For example, if I want to convert the rectangle CGRect(10,10,20,20) only but not the entire UIView, how do I do it?

推荐答案

您可以设置坐标转换矩阵。这允许您更改坐标系,因此当图层在(0,0)处绘制时,它实际上将在上下文中的其他位置渲染。您希望用于实际修改CTM的功能记录在转换用户空间部分。

You can set the coordinate transformation matrix on your context. This allows you to change the coordinate system, so when the layer draws at (0,0), it will actually render somewhere else in the context. The functions you want to use to actually modify the CTM are documented in the Transforming User Space section of the CGContext docs.

这篇关于如何只选择UIView的一部分转换为UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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