我正在尝试使用Quartz2D进行绘制,但我发现绘图在经过几个点之后就会滞后很多。因此,我想知道如何将当前上下文缓存到bmp或jpg或图层中,然后再次绘制到图层上以使图形更平滑。哪种方式最好的方法呢?谢谢!
答案 0 :(得分:1)
您可能希望尝试在后台线程中绘制位图,而不是在UIView的drawRect中绘制并尝试保存该绘图层。您永远不必清除位图,只能继续绘制它。那么你在UIView drawRect中所要做的只是一张可用的最新位图的图像绘制,iOS可以很快地完成。
答案 1 :(得分:0)
但是,如果要缓存当前上下文,代码如下所示:
// Begin image context
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
CGImageRef screenshotRef = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());
UIImage *screenshot = [[UIImage alloc] initWithCGImage:screenshotRef];
// End image context
UIGraphicsEndImageContext();
// Release CGImage
CGImageRelease(screenshotRef);
return screenshot;
答案 2 :(得分:0)
您可以先将一个图层转换为另一个图层,方法是先将不同的图层转换为图像,然后合并为一个单位。
//Draw path and return Image
let firstBeizerPath:UIImage = drawPath(firstBeizerpath: drawCircle())
let secondBeizerPath:UIImage = drawPath(firstBeizerpath: polygon())
//Draw different saved context into single image.
let image = UIImage(). drawOneLayerOverAnother(firstImage:firstBeizerPath , secondImage:secondBeizerPath)
extension UIImage {
func drawOneLayerOverAnother(firstImage:UIImage , secondImage:UIImage ) -> UIImage {
UIGraphicsBeginImageContext(firstLayer.size)
firstImage.draw(in: CGRect(x: 0, y: 0, width: firstLayer.size.width, height: firstLayer.size.height) )
secondImage.draw(in: CGRect(x: 0, y: 0, width: firstLayer.size.width, height: firstLayer.size.height) )
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}