0

I'm currently developing an app like photoshop for iPad and when i want flatten my array of layers, DrawTextInRect doesn't preserve the Transform (CAAffineTransformMakeRotation) of my UILabel. Anybody have any idea?

Hemang
  • 26,840
  • 19
  • 119
  • 186
Jonathan
  • 873
  • 3
  • 9
  • 23

2 Answers2

0

Draw your text into another context and get a CGImage of it, then see if drawing the image in your rotated context will work (it should).

EDIT: I have not done this myself, I believe it will work. The label probably should not be inserted into a view when you do this, but it might work that way anyway. You may have to experiment:

UIGraphicsBeginImageContextWithOptions( myLabel.bounds.size, NO, 0); // 0 == [UIScreen mainScreen].scale
CGContextRef context = UIGraphicsGetCurrentContext();

[myLabel.layer renderInContext:context];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
David H
  • 40,852
  • 12
  • 92
  • 138
  • I tried to create a function in a custom UIlabel Class wich return an image of my Label. When I put the function's code in the main context that's work but In the context create in my function not. I don't understand why... How do you create a new context in a current context ? – Jonathan Oct 09 '12 at 08:01
0

Translate the context to the label's origin, and then apply the label's transform to the context.

The code below is in Swift.

let context = UIGraphicsGetCurrentContext()!

context.saveGState()

let t = myLabel.transform
let translationPt = CGPoint.init(x: myLabel.frame.origin.x, y: myLabel.frame.origin.y)
context.translateBy(x: translationPt.x, y: translationPt.y)

context.concatenate(t)

myLabel.layer.render(in: context)

context.restoreGState()