GetImageFromCurrentImageContext时

GetImageFromCurrentImageContext时

本文介绍了使用UIGraphicsGetImageFromCurrentImageContext时正确的GraphicsContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下方法绘制图像:

I have the following method where I'm trying to do some drawing into an image:

- (UIImage*) renderImage
{
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //drawing code

    UIImage *image = [UIGraphicsGetImageFromCurrentImageContext() retain];
    UIGraphicsEndImageContext();
    return [image autorelease];
}

当我运行这段代码时,我发现我受到的打击比我只是在 UIView drawRect 中绘制此代码时做的。我是否在这里绘制了错误的图形上下文(即 CGContextRef context = UIGraphicsGetCurrentContext(); )?还是 UIGraphicsGetImageFromCurrentImageContext 比绘制 drawRect 昂贵得多?

When I run this code I noticed that I'm getting hit much harder than I did when I was simply drawing this code in drawRect of a UIView. Am I drawing into the wrong graphics context here (ie CGContextRef context = UIGraphicsGetCurrentContext();)? Or is UIGraphicsGetImageFromCurrentImageContext just that much more expensive than drawing in drawRect?

推荐答案

主要区别在于您创建的上下文需要屏幕外渲染,而不是在-drawRect中创建的上下文。因此,您要向堆添加额外的内存,直到释放映像为止。

The main difference is that the context that you create requires an offscreen rendering, it isn't the same context created in -drawRect. So you are adding an additional memory to the heap that stays until you will release the image.

这篇关于使用UIGraphicsGetImageFromCurrentImageContext时正确的GraphicsContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:33