Ios 使用RenderContext进行绘图,这种方式有助于UIView渲染

Ios 使用RenderContext进行绘图,这种方式有助于UIView渲染,ios,cocoa,uikit,core-graphics,Ios,Cocoa,Uikit,Core Graphics,我有一些UIView,它们有不同的中心和应用于它们的变换。 我想在位图上下文中重建这些视图。基本上,我希望将用户在屏幕上创建的内容呈现到电影文件中 我能够使在上下文中渲染的视图看起来几乎正确,但似乎存在偏移。我认为问题在于UIImageView的.center属性没有反映在我正在进行的转换中。但是我不知道怎么做。 请注意,UIView最初是相对于1024x768 ipad屏幕定位/转换的,其中视频缓冲区为352 x 288像素 如果我只是添加一个CGContextTranslateCTMnewC

我有一些UIView,它们有不同的中心和应用于它们的变换。 我想在位图上下文中重建这些视图。基本上,我希望将用户在屏幕上创建的内容呈现到电影文件中

我能够使在上下文中渲染的视图看起来几乎正确,但似乎存在偏移。我认为问题在于UIImageView的.center属性没有反映在我正在进行的转换中。但是我不知道怎么做。 请注意,UIView最初是相对于1024x768 ipad屏幕定位/转换的,其中视频缓冲区为352 x 288像素

如果我只是添加一个CGContextTranslateCTMnewContext,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);

}

下面是让它对我起作用的代码:注意1024768是因为UIImageView位于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);