2010-12-14 76 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