如何只select一部分UIView转换为UIImage

我有以下转换UIView到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; } 

但是,我只能select图像的大小,而不是在UIView应该被转换成图像的地方。 例如,如果我想只转换矩形CGRect(10,10,20,20),但不是整个UIView,我该怎么做?

您可以在您的上下文中设置坐标转换matrix 。 这允许你改变坐标系,所以当图层在(0,0)处绘制时,它实际上会渲染上下文中的其他地方。 您想用于实际修改CTM的函数logging在CGContext文档的“ 转换用户空间”部分。