I have some UIViews that have different centers and transforms applied to them.
I want to reconstruct these views on to a bitmap context. (Basically I want to take what the user has created on screen and render it on to a movie file)
I am able to get the view rendered in the context to look almost correct however there seems to be an offset. I am thinking the problem is that the UIImageView’s .center property is not reflected in the transforms that I am doing. However I am unsure how to do it.
Note that the UIViews are originally positioned/transformed relative to a 1024×768 ipad screen where as the video buffer is 352 x 288 pixels
If I just add a CGContextTranslateCTM(newContext,img.center.x,img.center.y) then everything looks completely off. Any ideas how to properly transform the view to the correct center?
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGContextRotateCTM(newContext, M_PI_2);
CGContextScaleCTM(newContext, 1, -1);
for(int i=0; i<[self.renderObjects count]; i++){
UIImageView * img = [self.renderObjects objectAtIndex:i];
[img setNeedsDisplay];
[img setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.2]];
CGContextSaveGState(newContext);
CGContextScaleCTM(newContext, 0.375, 0.34);
CGContextConcatCTM(newContext, img.transform);
[img.layer renderInContext:newContext];
CGContextRestoreGState(newContext);
}
Here is the code that made it work for me: note that the 1024, 768 is because the UIImageViews were positioned in the iPad coordinate system. The rotations are inverted though so if someone can find a general solution for that it would be great.