iOS到Mac GraphicContext说明/转换

dav*_*Mac 12 macos core-graphics converter cgcontext ios

我已经在iOS上编程了两年而从未在mac上编程.我正在开发一个小工具,用于处理我在iOS开发中的一些简单的图像需求.无论如何,我在iOS中运行的代码运行完美,但我完全不知道mac的等价物.

我尝试了很多不同的东西,但我真的不明白如何在"drawRect:"方法之外的Mac上启动图形上下文.在iPhone上我只会使用UIGraphicsBeghinImageContext().我知道其他帖子曾说过使用lockFocus/unlockFocus,但我不确定如何根据我的需要做到这一点.哦,我真的很想念UIImage的"CGImage"属性.我不明白为什么NSImage不能有一个,虽然听起来有点棘手.

这是我在iOS上的工作代码 - 基本上它只是从掩码中创建一个反射图像并将它们组合在一起:

    UIImage *mask = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Mask_Image.jpg" ofType:nil]];
    UIImage *image = [UIImage imageNamed::@"Test_Image1.jpg"];
    UIGraphicsBeginImageContextWithOptions(mask.size, NO, [[UIScreen mainScreen]scale]);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, 0.0, mask.size.height);
    CGContextScaleCTM(ctx, 1.f, -1.f);
    [image drawInRect:CGRectMake(0.f, -mask.size.height, image.size.width, image.size.height)];
    UIImage *flippedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGImageRef maskRef = mask.CGImage;
    CGImageRef maskCreate = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                              CGImageGetHeight(maskRef),
                                              CGImageGetBitsPerComponent(maskRef),
                                              CGImageGetBitsPerPixel(maskRef),
                                              CGImageGetBytesPerRow(maskRef),
                                              CGImageGetDataProvider(maskRef), NULL, false);
    CGImageRef masked = CGImageCreateWithMask([flippedImage CGImage], maskCreate);

    CGImageRelease(maskCreate);

    UIImage *maskedImage = [UIImage imageWithCGImage:masked];
    CGImageRelease(masked);

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.size.width, image.size.height + (image.size.height * .5)), NO, [[UIScreen mainScreen]scale]);

    [image drawInRect:CGRectMake(0,0, image.size.width, image.size.height)];
    [maskedImage drawInRect:CGRectMake(0, image.size.height, maskedImage.size.width, maskedImage.size.height)];

    UIImage *anotherImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext(); 

//do something with anotherImage
Run Code Online (Sandbox Code Playgroud)

在Mac上实现这个(简单)的任何建议?

cob*_*bal 21

这是一个简单的例子,它将一个蓝色圆圈绘制成一个NSImage(我在这个例子中使用ARC,添加保留/发布以品尝)

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];
Run Code Online (Sandbox Code Playgroud)

主要的区别是,在OS X上你首先必须创建图像,然后你可以开始绘制它; 在iOS上创建上下文,然后从中提取图像.

基本上,lockFocus使当前上下文成为图像,并直接在其上绘制,然后使用图像.

我不完全确定这是否能解决你的所有问题,但我认为这至少是其中的一部分.


jus*_*tin 10

好吧,这是关于UIGraphicsBeginImageContextWithOptions:

UIGraphicsBeginImageContextWithOptions

使用指定的选项创建基于位图的图形上下文.

OS X等效,也可以在iOS中使用(UIGraphicsBeginImageContextWithOptions可能是一个包装)CGBitmapContextCreate:

声明为:

CGContextRef CGBitmapContextCreate (
   void *data,
   size_t width,
   size_t height,
   size_t bitsPerComponent,
   size_t bytesPerRow,
   CGColorSpaceRef colorspace,
   CGBitmapInfo bitmapInfo
);
Run Code Online (Sandbox Code Playgroud)

虽然它是一个C API,但您可以将CGBitmapContext视为CGContext的子类.它呈现为像素缓冲区,而CGContext呈现为抽象目标.

因为UIGraphicsGetImageFromCurrentImageContext,您可以使用CGBitmapContextCreateImage并传递位图上下文来创建CGImage.


jes*_*.tt 8

这是Cobbal答案的Swift(2.1/10.11 API兼容)版本

    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()
Run Code Online (Sandbox Code Playgroud)