I have the following code:
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
My problem is that UIGraphicsGetImageFromCurrentImageContext returns nil, while the second one after UIGraphicsPopContext returns the correct result.
The docs clearly states that UIGraphicsGetImageFromCurrentImageContext will return nil when either the context is nil or the current context isn’t a graphic context, but both these problems aren’t happening here.
If anyone could shed some light over this i’d be very grateful
Shai.
From my understanding of things your issue is stemming from the following line
With this call you’re trying to make the current context the current context. This really doesn’t make any sense.
Also with your code it’s a little scrambled, you call UIGraphicsGetCurrentContext() which will return the current context, then you call UIGraphisBeginImageContext(CGSize size) which as stated in the doc’s
Then you get the current graphics context again which is now a bitmap-based graphics context thanks to the previous call, then you overwrite the original CGContextRef (“ctx”) that you just retrieved.
I’m not 100% certain what you were aiming to achieve with your code but if you were just trying to capture the contents of a bitmap-based context in an image and save it to the photo album then the following code will do that.
Hope that helps.