2011-12-26 27 views
8

好的使用coregraphics,我正在建立一個圖像,稍後將在CGContextClipToMask操作中使用。它看起來像下面這樣:如何從Context-Drawn Images獲取CGImageRef?

UIImage *eyes = [UIImage imageNamed:@"eyes"]; 
    UIImage *mouth = [UIImage imageNamed:@"mouth"]; 

    UIGraphicsBeginImageContext(CGSizeMake(150, 150)); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(context, 0, 0, 0, 1); 
    CGContextFillRect(context, bounds); 
    [eyes drawInRect:bounds blendMode:kCGBlendModeMultiply alpha:1]; 
    [mouth drawInRect:bounds blendMode:kCGBlendModeMultiply alpha:1]; 

    // how can i now get a CGImageRef here to use in a masking operation? 

    UIGraphicsEndImageContext(); 

現在,你可以通過評論看到,我想知道我怎麼竟要使用我已經建立起來的形象。之所以我在這裏使用核心圖形,而不是僅僅構建UIImage,是因爲我創建的透明度非常重要。如果我只是從上下文中獲取UIImage,當它用作掩碼時,它將適用於所有內容...更重要的是,使用此方法使用部分透明的掩碼會有什麼問題嗎?

+0

[Drawing into CGImageRef]的可能重複(http://stackoverflow.com/questions/4257043/drawing-into-cgimageref) – 2011-12-26 17:00:27

回答

12
CGImageRef result = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());