版权声明:本文为博主原创文章,转载请附上本文链接地址。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;
}