Drawing into CGImageRef

冷暖自知 提交于 2019-12-18 13:24:03

问题


I want to create a CGImageRef and draw points to it.

What context to use to create an empty CGImageRef and be able to draw onto it. CGContextRef or CGBitmapContextRef?

If you can provide code to create an empty CGImageRef image and draw to it I would appreciate.


回答1:


#define HIRESDEVICE (((int)rintf([[[UIScreen mainScreen] currentMode] size].width/[[UIScreen mainScreen] bounds].size.width )>1))


- (CGImageRef) blerg
    {
    CGFloat imageScale = (CGFloat)1.0;
    CGFloat width = (CGFloat)180.0;
    CGFloat height = (CGFloat)180.0;

    if ( HIRESDEVICE )
        {
        imageScale = (CGFloat)2.0;
        }

    // Create a bitmap graphics context of the given size
    //
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, width * imageScale, height * imageScale, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);


    // Draw ...
    //
    CGContextSetRGBFillColor(context, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)1.0 );
    // …


    // Get your image
    //
    CGImageRef cgImage = CGBitmapContextCreateImage(context);

    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);

    return cgImage;
    }


来源:https://stackoverflow.com/questions/4257043/drawing-into-cgimageref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!