2011-12-19 74 views
0

我有一些UIView,它们应用了不同的中心和变换。 我想重建这些视图上的位图上下文。 (基本上我想要拍摄用户在屏幕上创建的内容并将其渲染到电影文件中)以与UIView呈现类似的方式绘制使用renderInContext

我能够在上下文中看到呈现的视图差不多正确但是似乎存在偏移。我在想这个问题是UIImageView的.center属性没有反映在我正在做的转换中。但我不确定如何去做。 注意,UIViews最初被定位/相对于1024×768屏幕的ipad其中作为视频缓冲器是352×288像素

转化如果我只需添加一个CGContextTranslateCTM(newContext,img.center.x,img.center.y )然后一切看起来完全关闭。任何想法如何正确地将视图转换到正确的中心?

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); 

} 

回答

0

下面是使它适用于我的代码:请注意,1024,768是因为UIImageViews定位在iPad坐标系中。尽管如此,如果有人可以找到一个通用的解决方案,这将是很好的旋转。

 UIImageView * curr = your image 
    [curr setNeedsDisplay]; 
    CGContextSaveGState(newContext); 
    CGContextScaleCTM(newContext,height/768.0,width/1024.0); 
    CGContextTranslateCTM(newContext, 768-curr.center.x, curr.center.y); 
    CGContextConcatCTM(newContext, curr.transform); 
    CGContextTranslateCTM(newContext, -curr.bounds.size.width/2, -curr.bounds.size.height/2); 
    [curr.layer renderInContext:newContext]; 
    CGContextRestoreGState(newContext); 
相关问题