2

I have an pattern image(red texture with shadow on bottom). When I use this code

view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myPattern.png"]];

it's OK, and shadow is in bottom. But when I use

view.layer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"subscribe-pattern.png"]].CGColor;

it's a trouble. Image becomes fliped (shadow is on top). Can you tell me, how I can fix this problem? I need to have unflipped image using second method.

Padavan
  • 1,056
  • 1
  • 11
  • 32
  • The issue is that core graphics has its origin at the lower left corner, vs iOS which has its origin at the top left. – bobnoble Oct 29 '12 at 13:35
  • Possible duplicate of [CALayer - backgroundColor flipped?](http://stackoverflow.com/questions/5604531/calayer-backgroundcolor-flipped) – zxcat Feb 29 '16 at 13:18

1 Answers1

5

As bobnoble mentions in his comment, Core Graphics and iOS have flipped origins. iOS starts at the top left corner of the screen, and Core Graphics starts at the bottom left.

By accessing the layer property of a view you're dropping down to the Core Graphics level. If you must drop down to that level, you have to start working upside-down. An easy fix is to pre-flip your pattern. Just save it from your graphics editor upside-down. If you need to use the same image at both the UI and CG levels, you can always save two versions.

In code you can flip and translate the CG layer before drawing the upside-down image, then return the layer to its previous state for further drawing. It's kind of complex, but well covered by many Core Graphics tutorials.

Mr. Berna
  • 10,525
  • 1
  • 39
  • 42