How to create a CGBitmapContext which works for Retina display and not wasting space for regular display?

后端 未结 4 2003
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 08:41

Is it true that if it is in UIKit, including drawRect, the HD aspect of Retina display is automatically handled? So does that mean in drawRect, th

相关标签:
4条回答
  • 2020-12-08 09:02

    After doing more research, I found the following solution:

    If you have to use CGBitmapContextCreate, then there are two steps that can make the context with a size and coordinate system tailored to a standard display or Retina display:

    float scaleFactor = [[UIScreen mainScreen] scale];
    
    CGSize size = CGSizeMake(768, 768);
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    CGContextRef context = CGBitmapContextCreate(NULL, 
                               size.width * scaleFactor, size.height * scaleFactor, 
                               8, size.width * scaleFactor * 4, colorSpace, 
                               kCGImageAlphaPremultipliedFirst);
    
    CGContextScaleCTM(context, scaleFactor, scaleFactor);
    

    The sample is to create a 768 x 768 point region, and on The New iPad, it will be 1536 x 1536 pixel. On iPad 2, it is 768 x 768 pixel.

    A key factor is that, CGContextScaleCTM(context, scaleFactor, scaleFactor); is used to adjust the coordinate system, so that any drawing by Core Graphics, such as CGContextMoveToPoint, etc, will automatically work, no matter it is standard resolution or the Retina resolution.


    One more note is that UIGraphicsBeginImageContext(CGSizeMake(300, 300)); will create a 300 x 300 pixel on Retina display, while UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), NO, 0.0); will create 600 x 600 pixel on the Retina display. The 0.0 is for the method call to automatically give the proper size for standard display or Retina display.

    0 讨论(0)
  • 2020-12-08 09:09

    After beginning the new image context, you can retrieve it using UIGraphicsGetCurrentContext. Then, if you want to hang onto it and reuse it thereafter, just retain it like you would any CF object (and remember to release it when you're done with it, in accordance with the rules). You still have to call UIGraphicsEndImageContext to pop it off of UIKit's context stack, but if you've retained the context, then the context will live on afterward and you should be able to continue using it until you release it.

    Later, if you want to use the context again (and haven't released it yet), one way would be to call UIGraphicsPushContext, which will push the context back onto the context stack.

    The other way to use the context would be to assume it's a CGBitmapContext (the UIKit docs call it a “bitmap-based context” but don't say CGBitmapContext by name) and use CGBitmapContextCreateImage to capture a new image from the context after drawing.

    The main difference is that, if you've created the context with UIGraphicsCreateImageContextWithOptions, UIGraphicsGetImageFromCurrentImageContext returns a UIImage whose scale should match the value you created the context with. (I don't know whether that scale value gets preserved if you pop the context and then push it back later.) CGBitmapContextCreateImage returns a CGImage, and CGImage only knows pixels.

    The other difference is that UIKit drawing APIs, such as UIBezierPath, work on the current context in UIKit's context stack. Thus, if you don't push the context, you can only use Quartz APIs to draw into the context.

    I haven't tested any of the above, so you should test it thoroughly yourself before doing this in code that you will deliver to users.

    0 讨论(0)
  • 2020-12-08 09:09

    just create the context with scaling 0.0 as to get the main screen's with :

    UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    

    no third step.

    0 讨论(0)
  • 2020-12-08 09:11

    Also try this one:

    - (UIImage *)maskImageWithColor:(UIColor *)color
    {
        CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
        UIGraphicsBeginImageContextWithOptions(rect.size, NO, self.scale);
        CGContextRef c = UIGraphicsGetCurrentContext();
        [self drawInRect:rect];
        CGContextSetFillColorWithColor(c, [color CGColor]);
        CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
        CGContextFillRect(c, rect);
        UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题