macOS 开发 - 画图与 iOS 的几点细节区别

版权声明:本文为博主原创文章,转载请附上本文链接地址。from : https://blog.csdn.net/lovechris00 https://blog.csdn.net/lovechris00/article/details/83857523


UIGraphicsGetCurrentContext

iOS

CGContextRef contextRef = UIGraphicsGetCurrentContext();

Mac

CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];

生成图片

iOS

- (UIImage *)imageRepresentation {
    UIGraphicsBeginImageContext(self.frame.size);
    [[self layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

Mac

- (NSImage *)imageRepresentation {
    NSSize mySize = self.bounds.size;
    NSSize imgSize = NSMakeSize( mySize.width, mySize.height );
    NSBitmapImageRep *bir = [self bitmapImageRepForCachingDisplayInRect:[self bounds]];
    [bir setSize:imgSize];
    [self cacheDisplayInRect:[self bounds] toBitmapImageRep:bir];
    NSImage* image = [[NSImage alloc]initWithSize:imgSize];
    [image addRepresentation:bir];
    return image;
}

猜你喜欢

转载自blog.csdn.net/lovechris00/article/details/83857523