2011-05-03 88 views

回答

26

像這樣:

-(UIImage*)doImageOperation 
{ 
    // Do your stuff here 
    CGImageRef imgRef = CGBitmapContextCreateImage(context); 
    UIImage* img = [UIImage imageWithCGImage:imgRef]; 
    CGImageRelease(imgRef); 
    CGContextRelease(context); 
    return img; 
} 
+0

由於完成了!有了這個,我創建了一個方便的方法'UIImage * UIGraphicsGetImageFromImageContext(CGContextRef context)'來使用。 – samwize 2011-05-03 08:09:01

+1

這隻會在'context'是一個CGBitmapContext;它可能不適用於任何其他類型的CGContext。 @samwize:你應該爲它命名一些不同的東西。前綴是命名空間的約定; Apple可能會隨時添加一個名爲'UIGraphicsGetImageFromImageContext'的函數,公共或私有函數,如果他們這樣做,則會導致問題。使用你自己的前綴。 – 2011-05-03 14:32:04

+0

感謝您的建議! – samwize 2011-05-04 06:09:54

2

更新了夫特3,這是一個方便的功能,需要一個CGContext上並返回一個UIImage。請注意,您不再需要釋放的背景下,當你用它在斯威夫特3.

func imageFromContext(_ context: CGContext) -> UIImage? { 
    guard let cgImage = context.makeImage() else { return nil } 
    return UIImage.init(cgImage: cgImage) 
}