How to create a UIImage from the current graphics context?

I think you may need to setup the graphics context first: UIGraphicsBeginImageContext(myView.bounds. Size) myView. Layer renderInContext:UIGraphicsGetCurrentContext() viewImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext().

I think you may need to setup the graphics context first: UIGraphicsBeginImageContext(myView.bounds. Size); myView. Layer renderInContext:UIGraphicsGetCurrentContext(); viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext().

Thanks for the response, however doesn't seem to work. I added the line: _signatureImage = UIGraphicsGetImageFromCurrentImageContext(); ... where _signatureImage is a retained property. The returned object is nil.

According to the docs, UIGraphicsGetImageFromCurrentImageContext() is intended for use only if the current context is a bitmap context. In my current implementation of drawRect: I don't believe that's the case. The docs mention the use of UIGraphicsBeginImageContext(), but I'm not sure how that applies in my case because drawRect may be invoked multiple times.

– figelwump Jul 11 '09 at 5:43.

I think you may need to setup the graphics context first.

Up vote 7 down vote favorite 3 share g+ share fb share tw.

I'd like to create a UIImage object from the current graphics context. More specifically, my use case is a view that the user can draw lines on. They may draw incrementally.

After they are done, I'd like to create a UIImage to represent their drawing. Here is what drawRect: looks like for me now: - (void)drawRect:(CGRect)rect { CGContextRef c = UIGraphicsGetCurrentContext(); CGContextSaveGState(c); CGContextSetStrokeColorWithColor(c, UIColor blackColor. CGColor); CGContextSetLineWidth(c,1.5f); for(CFIndex I = 0; I Also note that drawRect: may be invoked several times, as the user draws.

When the user is done, I'd like to create a UIImage object that represents the graphics context. Any suggestions on how to do this? Iphone uiimage core-graphics quartz-graphics link|improve this question asked Jul 11 '09 at 4:26figelwump25026 60% accept rate.

Both of the answers below work. Note that there is a functional difference between the two, which is that rendering the layer in the image context will also render the layer's background; calling drawRect: directly does not (in my case it doesn't matter). I am going with the CALayer renderInContext approach because of complexities with calling drawRect: directly (See lists.apple.com/archives/Cocoa-dev/2002/... for details).

Thanks! – figelwump Jul 11 '09 at 17:41.

UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); If you need to keep image around, be sure to retain it! EDIT: If you want to save the output of drawRect to an image, just create a bitmap context using UIGraphicsBeginImageContext and call your drawRect function with the new context bound. It's easier to do than saving the CGContextRef you're working with within drawRect - because that context may not have bitmap information associated with it.

UIGraphicsBeginImageContext(view.bounds. Size); view drawRect: myView bounds; UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); You can also use the approach that Kelvin mentioned. If you want to create an image from a more complex view like UIWebView, his method is faster.

Drawing the view's layer does not require refreshing the layer, it just requires moving image data from one buffer to another!

A uiimage from the current graphics context is created (without quotes):. Create a UIImage from the current graphics context.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions