iOS:在上下文中多次绘制图像

iOS:在上下文中多次绘制图像,ios,graphics,core-graphics,Ios,Graphics,Core Graphics,我有一些图像,我希望在我的UIControl中绘制它。但为了提高性能,我需要在图形环境中绘制 现在我可以用这样的方法: var imageRect1 = CGRectMake(0, 0, width, height) var imageRect2 = CGRectMake(x2, y2, width, height) ... CGContextDrawImage(ctx, imageRect1, image) CGContextDrawImage(ctx, imageRect2, image)

我有一些图像,我希望在我的UIControl中绘制它。但为了提高性能,我需要在图形环境中绘制

现在我可以用这样的方法:

var imageRect1 = CGRectMake(0, 0, width, height)
var imageRect2 = CGRectMake(x2, y2, width, height)
...
CGContextDrawImage(ctx, imageRect1, image)
CGContextDrawImage(ctx, imageRect2, image)
...
但这个解决方案意味着,每次调用上下文函数时,我们至少要读取
image
。 我搜索一些解决方案,这些解决方案需要一些步骤,比如下一步(如果我错了,请随时纠正我):


也许我必须逐像素复制?有人能给我一些如何快速绘制的建议吗?

如果要在CALayer中绘制单个图像,最好的解决方案是将图像设置为backing CALayer的内容属性,如中所述

否则如果您担心的是使用
CGImageRef
CGContextRef
进行缓存,您的问题已经在这里得到了回答:

否则,如果您直接在
ui视图中绘制,请参见@boeqsh注释:

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)

你想做什么?如果要绘制图案/平铺图像,有更好的方法可以使用
[uicolorWithPatternImage:
cgContextDrawTileImage
。如果您需要直接控制每个图像的显示位置,您可以使用
CGLayer
。假设我有一些东西,需要插入500次图像。所以我试着想办法不把图像读500遍。可能读取图像位图数组中的第一个像素,并将其分配到结果位图数组中的500位置?我不知道应该使用哪种方法来获得一些有价值的性能,我应该如何做到。你也可以使用
drawInRect
drawAtPoint
方法,然后从当前上下文中获取图像。@VitaliyLevytskyy:出于好奇,你最终是如何解决这个问题的?
self.imageLayer.contents = (id)image.CGImage
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)