有没有可能从UIView中创建一个图像?
谢谢,安德烈亚斯
发布于 2010-08-16 16:56:55
在您将框架添加到您的项目之后使用#import "QuartzCore/QuartzCore.h"
。然后执行以下操作:
UIGraphicsBeginImageContext(yourView.frame.size);
[[yourView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// The result is *screenshot
发布于 2010-08-17 20:55:15
我已经使用了答案来进一步改进它。不幸的是,原始代码只生成了镜像图像。
下面是工作代码:
- (UIImage *) imageFromView:(UIView *)view {
UIGraphicsBeginImageContext(view.frame.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 0, view.size.height);
// passing negative values to flip the image
CGContextScaleCTM(currentContext, 1.0, -1.0);
[[appDelegate.scatterPlotView layer] renderInContext:currentContext];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenshot;
}
https://stackoverflow.com/questions/3495370
复制相似问题