我想从我的 iPhone 应用程序中捕获我的 View 屏幕。我有白色背景 View ,并使用此方法在该 View 的图层上画了一条线。
- (void)draw {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
if (self.fillColor) {
[self.fillColor setFill];
[self.path fill];
}
if (self.strokeColor) {
[self.strokeColor setStroke];
[self.path stroke];
}
CGContextRestoreGState(context);
}
- (void)drawRect:(CGRect)rect {
// Drawing code.
for (<Drawable> d in drawables) {
[d draw];
}
[delegate drawTemporary];
}
我使用委托(delegate)方法在图层上画线。 这是我从中获得帮助的项目链接。 https://github.com/search?q=dudel&type=Everything&repo=&langOverride=&start_value=1
现在,当我使用以下上下文方法保存绘图时,我成功地保存了它,没有那个白色背景。
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
但是当我使用以下 Bezier Pathe 方法时,我无法在没有白色背景的情况下保存绘图,它会保存整个屏幕,即该绘图及其背景。
UIGraphicsBeginImageContext(self.view.bounds.size);
[dudelView.layer renderInContext:UIGraphicsGetCurrentContext()];
//UIImage *finishedPic = UIGraphicsGetImageFromCurrentImageContext();
那么谁能帮我我怎么才能在这个应用程序中只保存这里的绘图。
最佳答案
(您已将此标记为与 MapKit 相关,但未提及 MapKit。)
为什么不将绘图序列分成三个 block ?
- 按照您的描述将您的路径绘制到图像上下文中并获取 UIImage。
- 绘制背景色。
- 绘制 UIImage。
然后您也可以将 UIImage 用于您的“屏幕截图”。
我还应该注意,如果您在捕获的 UIImage 中唯一不想要的是背景颜色,您最好创建一个 UIImageView,设置它的背景颜色(- setBackgroundColor:
),并将其图像设置为您的 UIImage。
UIImageView 内部有许多优化,使其能够以比自定义 -drawRect:
实现更高的性能显示图形。
关于ipad - Iphone sdk 在没有整个上下文的情况下保存图纸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7631243/