CGContextRotateCTM没有显示任何内容

时间:2011-07-27 05:43:51

标签: iphone

- (void)drawRect:(CGRect)rect {
    // Drawing code.
    stickerImage = [UIImage imageNamed:@"betaImage.png"];
    CGSize size = stickerImage.size;
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM( context, 0.5f * size.width, 0.5f * size.height );
    CGContextRotateCTM (context, 90 * M_PI/180);
    [stickerImage drawInRect:(CGRect){ { -size.width * 0.5f, -size.height * 0.5f }, size }];

    UIGraphicsEndImageContext();
}

我无法找到我的代码有什么问题,它没有显示任何内容

2 个答案:

答案 0 :(得分:1)

不要开始和结束图像上下文 - 用于绘制到UIImage。当前上下文已经设置为drawRect中视图的上下文--UIGraphicsGetCurrentContext()将提供开箱即用的正确上下文。

换句话说 - 摆脱UIGraphicsBeginImageContext()和UIGraphicsEndImageContext()。

答案 1 :(得分:0)

是的,在获得结果图像之前,您不应该结束上下文。在您的代码示例中,您应该获得结果图像:

UIImage* yourImage = UIGraphicsGetImageFromCurrentImageContext();

或者只是将结束上下文传递给其他父方法;

正如Morgan Harris所说,所有图像操作都应该在UIGraphicsBeginImageContext()和UIGraphicsEndImageContext()之间;

相关问题