gpt4 book ai didi

iphone - UIGraphicsGetImageFromCurrentImageContext返回意外的nil

转载 作者:行者123 更新时间:2023-12-01 18:01:57 24 4
gpt4 key购买 nike

我有以下代码:

CGContextRef ctx = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContext(size);
screenImageContext = UIGraphicsGetCurrentContext();
ctx = screenImageContext;

UIGraphicsPushContext(UIGraphicsGetCurrentContext());
ctx = UIGraphicsGetCurrentContext();

NSLog(@" %@",screenImageContext);
UIImage * result = UIGraphicsGetImageFromCurrentImageContext(); // Returns nil

UIImageWriteToSavedPhotosAlbum(result, nil, nil, nil);

UIGraphicsPopContext();
result = UIGraphicsGetImageFromCurrentImageContext(); // returns valid result

我的问题是 UIGraphicsGetImageFromCurrentImageContext返回nil,而 UIGraphicsPopContext之后的第二个返回正确的结果。

文档明确指出,当上下文为nil或当前上下文不是图形上下文时, UIGraphicsGetImageFromCurrentImageContext将返回nil,但是这两个问题均未在此处发生。

如果有人能对此有所启示,我将非常感谢

最佳答案

根据我对事物的了解,您的问题源自以下几行

UIGraphicsPushContext(UIGraphicsGetCurrentContext());

通过此调用,您尝试将当前上下文设为当前上下文。这真的没有任何意义。

同样,您的代码有些混乱,您调用UIGraphicsGetCurrentContext(),它将返回当前上下文,然后调用UIGraphisBeginImageContext(CGSize size),如文档的

创建基于位图的图形上下文并将其作为当前上下文

然后,由于先前的调用,您再次获得了当前的图形上下文,该上下文现在是基于位图的图形上下文,然后覆盖了刚刚检索的原始CGContextRef(“ctx”)。

我不是100%地确定您要用代码实现的目标,但是如果您只是试图捕获图像中基于位图的上下文的内容并将其保存到相册中,则下面的代码可以做到这一点。
CGSize size = CGSizeMake(320, 480); //Screen Size on iPhone device
UIGraphicsBeginImageContext(size); //Create a new Bitmap-based graphics context (also makes this the current context)
CGContextRef screenImageContext = UIGraphicsGetCurrentContext(); //get a reference to the context we just made above
NSLog(@" %@",screenImageContext);
//NOTE: without any drawring code in here this will just be a blank image (white/alpha)
// or an image set to whatever the current UIColor is set to
//So you may want to add some drawing code in here. Although TBH I'm not sure what you were originally
// trying to achieve.
UIImage * result = UIGraphicsGetImageFromCurrentImageContext(); // Returns nil
NSLog(@" %@",result); //just output this to demonstrate that it's non null/nil
UIImageWriteToSavedPhotosAlbum(result, nil, nil, nil);
UIGraphicsEndImageContext(); //Removes the current bitmap-based graphics context from the top of the stack

希望能有所帮助。

关于iphone - UIGraphicsGetImageFromCurrentImageContext返回意外的nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8255722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com