我有一些图像,希望在UIControl中绘制它。但是为了提高性能,我需要在图形环境中进行绘制。
现在我可以使用类似的东西:
var imageRect1 = CGRectMake(0, 0, width, height)
var imageRect2 = CGRectMake(x2, y2, width, height)
...
CGContextDrawImage(ctx, imageRect1, image)
CGContextDrawImage(ctx, imageRect2, image)
...
但是这种解决方案意味着,每次调用该上下文函数时,我们至少要阅读
image
。我搜索了一些可以通过某些步骤使用的解决方案,例如下一个(如果我错了,请随时纠正我):
//read image into some bitmap or maybe some byte array
//paste it several times in different places (several times means ~500 times)
也许我必须逐像素复制?有人可以给我一些建议,以快速完成它吗?
最佳答案
如果可以在CALayer中绘制单个图像,则最有效的解决方案是按照Technical Q&A QA1708中所述将图像设置为支持CALayer的contents属性。
self.imageLayer.contents = (id)image.CGImage
否则,如果您的问题是使用
CGImageRef
和CGContextRef
进行缓存,则您的问题已在此处得到解答:UIImage -> CGImageRef
,如果是,则直接用
UIView
绘制,请参见@boeqqsh注释:let image = UIImage.imageNamed("image")
let imageRect1 = CGRectMake(0, 0, width, height)
let imageRect2 = CGRectMake(x2, y2, width, height)
image.drawInRect(imageRect1)
image.drawInRect(imageRect2)
关于ios - iOS:在上下文中多次绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25401127/