1

The Question is :

When working on iPhone7,

I get CGContextRef from UIView, then the bitsPerPixel is 64. Cause my .a can only work with traditional RGB colorspace bitmap buffer whose bitsPerPixel is 32,

Could someone know how to convert CGContextRef whose colorspace is wide color to tranditional CGContextRef just like on iPhone6 ?

Anptk
  • 1,125
  • 2
  • 17
  • 28
  • This is just an idea. Okay, so wide color is a trait (UIDisplayGamut). So perhaps you could use a custom container view controller so that you could call `setOverrideTraitCollection(_:forChildViewController:)` and fool your child view controller into believing it's running on a device without wide color. – matt Oct 25 '16 at 03:02
  • @matt Thanks a lot for anwser, I'll have a try. Yesterday, I find a way to deal with wide color, set the UIView's layer property "contentsFormat" to kCAContentsFormatRGBA8Uint, then the CGContextRef got from runtime is 32 bitsPerPixel – CoderHunter Oct 27 '16 at 02:38
  • If that works, you should post it as an answer! This wide color stuff is certainly going to cause some confusion, so your work here could be very useful to others. (Also please note that I don't know whether my idea will work! I need you to try it for me.) – matt Oct 27 '16 at 02:43
  • @matt I've tried your idea, but it seems failed. But the way I found just work fine. – CoderHunter Oct 28 '16 at 03:41
  • That is great. Please post it as an answer! – matt Oct 28 '16 at 03:47

1 Answers1

0

The Solution is below:

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_9_3
-(void) layerWillDraw:(CALayer *)layer 
{
    if( [[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0 )
    {
        NSString* format = layer.contentsFormat;
        if( ![format isEqualToString:kCAContentsFormatRGBA8Uint]  )
            layer.contentsFormat = kCAContentsFormatRGBA8Uint;
    }
}
#endif

add the code above to the view which you will draw on, then the CGContextRef got from the view is traditional, just as on iPhone6