【问题标题】:Drawing using renderInContext in a way that is analagous to UIView rendering以类似于 UIView 渲染的方式使用 renderInContext 进行绘制
【发布时间】:2011-12-19 10:49:15
【问题描述】:

我有一些 UIViews 应用了不同的中心和变换。 我想将这些视图重建到位图上下文中。 (基本上我想将用户在屏幕上创建的内容渲染到电影文件中)

我能够使在上下文中呈现的视图看起来几乎正确,但似乎存在偏移。我在想问题是 UIImageView 的 .center 属性没有反映在我正在做的转换中。但是我不确定该怎么做。 请注意,UIView 最初是相对于 1024x768 ipad 屏幕定位/转换的,其中视频缓冲区为 352 x 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);

}

【问题讨论】:

    标签: ios cocoa uikit core-graphics


    【解决方案1】:

    这是使它对我有用的代码:请注意,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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2014-02-10
      • 2019-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多