2010-12-14 67 views
6
CGContextRef context = ??; // get from UIImage *oldImage 

    CGContextSaveGState(context); 
    CGRect drawRect = CGRectMake(0, 0, 320, 480); 
    CGContextConcatCTM(context, transform); 
    UIImage *newImage = [[UIImage alloc] init]; 
    CGContextDrawImage(context, drawRect, newImage.CGImage); 
    [newImage drawInRect:CGRectMake(0, 0, 320, 480)]; 

    CGContextRestoreGState(context);

总之,我想要做的是[UIImage - >做一些转换 - >转换UIImage]。如何从UIImage获取CGContextRef?

任何想法?十分感谢!!

回答

8

我想你需要的是这样的:

UIGraphicsBeginImageContextWithOptions(newImageSize, YES, 0); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
// drawing commands go here 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

注意UIGraphicsGetImageFromCurrentImageContext()返回一个自动释放UIImage的实例。

+0

谢谢,但即使我什么都没做,我又得到了一张颠倒的图像? – 2010-12-14 15:03:22

+0

如果您使用Core Graphics进行绘图(例如'CGContextDrawImage()'),请记住它使用翻转的坐标系。 UIImage绘图方法不需要预翻转坐标系统。 – Costique 2010-12-14 17:09:45

+0

Noob问题:这看起来非常浪费......我只想在我的图像上追加一些数据。我讨厌为此重绘整个事情。 – Ralphleon 2011-03-19 21:59:12