2011-05-03 53 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) 
}