Correct GraphicsContext When Using UIGraphicsGetImageFromCurrentImageContext

狂风中的少年 提交于 2019-12-11 08:53:32

问题


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];    
}

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?


回答1:


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.



来源:https://stackoverflow.com/questions/13173933/correct-graphicscontext-when-using-uigraphicsgetimagefromcurrentimagecontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!