1

我正在尝试使用 renderInContext 将视图层绘制到 CGGraphicsImageContext,但是视图层未显示在生成的 UIImage 上。

UIGraphicsBeginImageContext(scaledImage.size);
[scaledImage drawInRect:CGRectMake(0, 0, scaledImage.size.width, scaledImage.size.height)];


[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *croppedImage = [UIGraphicsGetImageFromCurrentImageContext() croppedImage:CGRectMake(0, 44, 320, scaledImage.size.height - (60 + 44))];
UIGraphicsEndImageContext();

我的视图 drawRect 方法如下:

-(void)drawRect:(CGRect)rect
{
    CALayer *background = [[CALayer alloc] init];
    background.backgroundColor = [UIColor blackColor].CGColor;
    background.opacity = 0.4;
    background.frame = self.bounds;

    [self.layer addSublayer:background];

    CATextLayer *textLayer = [[CATextLayer alloc] init];
    textLayer.string = _poseName;
    textLayer.fontSize = 19;
    textLayer.foregroundColor = [UIColor whiteColor].CGColor;
    textLayer.alignmentMode = kCAAlignmentCenter;
    textLayer.frame = self.bounds;

    CGSize textSize = [_poseName sizeWithFont:[UIFont fontWithName:@"Helvetica" size:19]
                        constrainedToSize:textLayer.bounds.size
                            lineBreakMode:NSLineBreakByWordWrapping];

    textLayer.position = CGPointMake(160, self.bounds.size.height - textSize.height/2);

    [self.layer addSublayer:textLayer];
}

当应用程序运行时,视图的图层可以正确显示,但是,视图并没有像我预期的那样呈现到图形上下文。预先感谢您的帮助!

4

0 回答 0