iOS to Mac GraphicContext Explanation/Conversion

前端 未结 4 918
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 05:57

I have been programming for two years on iOS and never on mac. I am working on a little utility for handling some simple image needs that I have in my iOS development. Anywa

相关标签:
4条回答
  • 2020-12-30 06:30

    Well, here's the note on UIGraphicsBeginImageContextWithOptions:

    UIGraphicsBeginImageContextWithOptions

    Creates a bitmap-based graphics context with the specified options.

    The OS X equivalent, which is also available in iOS (and UIGraphicsBeginImageContextWithOptions is possibly a wrapper around) is CGBitmapContextCreate:

    Declared as:

    CGContextRef CGBitmapContextCreate (
       void *data,
       size_t width,
       size_t height,
       size_t bitsPerComponent,
       size_t bytesPerRow,
       CGColorSpaceRef colorspace,
       CGBitmapInfo bitmapInfo
    );
    

    Although it's a C API, you could think of CGBitmapContext as a subclass of CGContext. It renders to a pixel buffer, whereas a CGContext renders to an abstract destination.

    For UIGraphicsGetImageFromCurrentImageContext, you can use CGBitmapContextCreateImage and pass your bitmap context to create a CGImage.

    0 讨论(0)
  • 2020-12-30 06:30

    Here is a Swift (2.1 / 10.11 API-compliant) version of Cobbal's answer

        let size = NSMakeSize(50, 50);
        let im = NSImage.init(size: size)
    
        let rep = NSBitmapImageRep.init(bitmapDataPlanes: nil,
            pixelsWide: Int(size.width),
            pixelsHigh: Int(size.height),
            bitsPerSample: 8,
            samplesPerPixel: 4,
            hasAlpha: true,
            isPlanar: false,
            colorSpaceName: NSCalibratedRGBColorSpace,
            bytesPerRow: 0,
            bitsPerPixel: 0)
    
        im.addRepresentation(rep!)
        im.lockFocus()
    
        let rect = NSMakeRect(0, 0, size.width, size.height)
        let ctx = NSGraphicsContext.currentContext()?.CGContext
        CGContextClearRect(ctx, rect)
        CGContextSetFillColorWithColor(ctx, NSColor.blackColor().CGColor)
        CGContextFillRect(ctx, rect)
    
        im.unlockFocus()
    
    0 讨论(0)
  • 2020-12-30 06:46

    Here's a simple example that draws a blue circle into an NSImage (I'm using ARC in this example, add retains/releases to taste)

    NSSize size = NSMakeSize(50, 50);
    
    NSImage* im = [[NSImage alloc] initWithSize:size];
    NSBitmapImageRep* rep = [[NSBitmapImageRep alloc]
                                initWithBitmapDataPlanes:NULL
                                              pixelsWide:size.width
                                              pixelsHigh:size.height
                                           bitsPerSample:8
                                         samplesPerPixel:4
                                                hasAlpha:YES
                                                isPlanar:NO
                                          colorSpaceName:NSCalibratedRGBColorSpace
                                             bytesPerRow:0
                                            bitsPerPixel:0];
    
    [im addRepresentation:rep];
    
    [im lockFocus];
    
    CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
    CGContextClearRect(ctx, NSMakeRect(0, 0, size.width, size.height));
    CGContextSetFillColorWithColor(ctx, [[NSColor blueColor] CGColor]);
    CGContextFillEllipseInRect(ctx, NSMakeRect(0, 0, size.width, size.height));
    
    [im unlockFocus];
    
    [[im TIFFRepresentation] writeToFile:@"/Users/USERNAME/Desktop/foo.tiff" atomically:NO];
    

    The main difference is that on OS X you first have to create the image, then you can begin drawing into it; on iOS you create the context, then extract the image from it.

    Basically, lockFocus makes the current context be the image and you draw directly onto it, then use the image.

    I'm not completely sure if this answers all of your question, but I think it's at least one part of it.

    0 讨论(0)
  • 2020-12-30 06:47

    Swift3 version of Cobbal's answer

       let size = NSMakeSize(50, 50);
        let im = NSImage.init(size: size)
    
        let rep = NSBitmapImageRep.init(bitmapDataPlanes: nil,
                                        pixelsWide: Int(size.width),
                                        pixelsHigh: Int(size.height),
                                        bitsPerSample: 8,
                                        samplesPerPixel: 4,
                                        hasAlpha: true,
                                        isPlanar: false,
                                        colorSpaceName: NSCalibratedRGBColorSpace,
                                        bytesPerRow: 0,
                                        bitsPerPixel: 0)
    
    
       im.addRepresentation(rep!)
        im.lockFocus()
    
        let rect = NSMakeRect(0, 0, size.width, size.height)
        let ctx = NSGraphicsContext.current()?.cgContext
        ctx!.clear(rect)
        ctx!.setFillColor(NSColor.black.cgColor)
        ctx!.fill(rect)
    
        im.unlockFocus()
    
    0 讨论(0)
提交回复
热议问题