2

I have some image, and i wish to draw it in my UIControl. But for increasing performance i need to draw in graphic context.

Now i can use something like that:

var imageRect1 = CGRectMake(0, 0, width, height)
var imageRect2 = CGRectMake(x2, y2, width, height)
...
CGContextDrawImage(ctx, imageRect1, image)
CGContextDrawImage(ctx, imageRect2, image)
...

But this solution means that we at least read image every time that call that context function. I search some solution that would work with some steps, like next one (feel free to correct me if I am wrong):

//read image into some bitmap or maybe some byte array
//paste it several times in different places (several times means ~500 times)

Maybe I have to copy pixel by pixel? Can someone give me some advice how to do it fast?

Vitaliy L
  • 572
  • 4
  • 20
  • 1
    what do you want to archieve? If you want to draw a pattern/tiled image there a better ways using `[UIColor colorWithPatternImage:]` or `CGContextDrawTiledImage`. If you need direct control over where every image appears you could use `CGLayer`. – Jonathan Cichon Aug 20 '14 at 09:35
  • Imagine that i have something [like that](http://i.stack.imgur.com/NnPOu.png) And i need to insert 500 times image. So I try to find some way do not read image 500 times. Maybe read first pixel in my image bitmap array and assign it in 500 position in result bitmap array? I don't know which way i should use for gain some valuable performance, and how should i do it.. – Vitaliy L Aug 20 '14 at 09:47
  • 1
    You could also use the `drawInRect` or the `drawAtPoint` method and then get an image from the current context. –  Sep 02 '14 at 11:25
  • @VitaliyLevytskyy: Out of curiosity, how did you eventually solve this issue? – SwiftArchitect Jan 31 '16 at 02:23

1 Answers1

0

if you have a single image to draw in a CALayer, the über most solution is to set the image as the contents property of the backing CALayer as described in Technical Q&A QA1708.

self.imageLayer.contents = (id)image.CGImage

else if your concern is caching using CGImageRef and CGContextRef, your question has already been answered here:

UIImage -> CGImageRef

else if you are drawing directly in a UIView, see @boeqqsh comment:

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)
Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179