RenderInContext flips the beginning of colorWithPatternImage

I have a UIView with backgroundColor with colorWithPatternImage . As expected, a background image is drawn starting from the top left corner.

The problem occurs when I make renderInContext in this view: the background image is drawn starting from the left corner at the bottom . Everything else seems beautiful.

Here are the source and target images:

enter image description here

Here is the code:

 // here is the layer to be rendered into an image UIView *src = [[UIView alloc] initWithFrame:(CGRect){{0, 0}, {100, 100}}]; src.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]; [self.view addSubview:src]; // here we'll display the image UIImageView *dest = [[UIImageView alloc] initWithFrame:(CGRect){{110, 0}, src.bounds.size}]; [self.view addSubview:dest]; // render `src` to an image in `dest` UIGraphicsBeginImageContext(src.bounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); [src.layer renderInContext:context]; dest.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

Is there a way to save the image in the right direction, as in the src view?

+7
source share
2 answers

I managed to get around this by calling CGContextSetPatternPhase with a height of modf (viewHeight, patternHeight)

+6
source

https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_overview/dq_overview.html#//apple_ref/doc/uid/TP30001066-CH202-CJBBAEEC

This document explains why this happens, in particular this part:

Important. The above discussion needs to be understood if you plan to write applications that directly target Quartz on iOS, but thatโ€™s not enough. On iOS 3.2 and later, when UIKit creates a drawing context for your application, it also makes additional changes to the context to comply with the default UIKIt conventions. In particular, patterns and shadows that are not affected by CTM are configured separately so that their conventions match the UIKits coordinate system. In this case, there is no equivalent mechanism for CTM that your application can use to change the context created by Quartz to match the behavior for the context provided by UIKit; Your application must recognize the context in which it fits and adjust its behavior to match the expectations of the context.

+2
source

All Articles