Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIGraphicsBeginImageContext vs CGBitmapContextCreate on iOS

This may be a really stupid question, but can someone tell me the difference between creating a CGContextRef using UIGraphicsBeginImageContext and using CGBitmapContextCreate for drawing to images? Especially now since UIKit drawing is thread safe I was wondering if there was any reason to use CGBitmapContextCreate over UIGraphicsBeginImageContext.

like image 796
marchinram Avatar asked Dec 15 '10 15:12

marchinram


1 Answers

According to Apple's UIKit Function Reference, which carries the cover date of the 15th November 2010, UIGraphicsBeginImageContext and related functions should still be called on the main thread only. The same text is repeated in the developer documentation that comes with the latest Xcode, 3.2.5. However, it reports the same for UIGraphicsGetCurrentContext which I explicitly understood to be thread safe now. My understanding was that only UIGraphicsGetCurrentContext and the UIImage, UIColor and UIFont classes are now thread safe rather than the entirety of UIKit, but I'm unable to find a definitive reference.

Regardless, UIGraphicsBeginImageContext is a UIKit wrapper that sits on top of CGBitmapContextCreate and reduces its functionality. In particular you're limited to RGBA colour space images with a fixed component order (though it varies according to the iOS version) and cannot specify your own target buffer for drawing. So, for example, it's useless for doing a bunch of CoreGraphics composition and then posting the result off to OpenGL and unhelpful for piping graphics you've already got in some array form into CoreGraphics.

However, where the UIKit method supports the functionality you need and is safe to use, there is no inherent advantage to the CoreGraphics methods.

like image 179
Tommy Avatar answered Sep 29 '22 15:09

Tommy