首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将CGLayer保存为PNG文件的正确代码是什么?

将CGLayer保存为PNG文件的正确代码是什么?
EN

Stack Overflow用户
提问于 2010-10-31 18:27:45
回答 1查看 2K关注 0票数 7

请注意,这个问题是关于CGLayer (通常用于屏幕外绘制),而不是CALayer。

在iOS中,将CGLayer保存为PNG文件的正确代码是什么?谢谢!

再说一遍,那是CGLayer,而不是CALayer。

请注意,不能使用UIGraphicsGetImageFromCurrentImageContext。

(在文档中,“只有当基于位图的图形上下文是当前图形上下文时,才能调用UIGraphicsGetImageFromCurrentImageContext”)。

请注意,不能使用renderInContext:。renderInContext:严格来说是为CALayers服务的。CGLayers完全不同。

那么,如何将CGLayer转换为PNG映像呢?或者,如何以某种方式将CGLayer呈现到位图中(当然,您可以轻松地将其保存为图像)。

以后..。肯回答了这个难题。我将粘贴一个长的示例代码,可以帮助人们。再次感谢肯!太棒了!

代码语言:javascript
复制
-(void)drawingExperimentation
{
// this code uses the ASTOUNDING solution by KENNYTM -- Oct/Nov2010
//
// create a CGLayer for offscreen drawing
// note. for "yourContext", ideally it should be a context from your screen, ie the
// context you "normally get" in one of your drawRect routines associated with
// drawing to the screen normally.
// UIGraphicsGetCurrentContext() also normally works but you could have colorspace woes

// so create the CGLayer called notepad...

CGLayerRef notepad = CGLayerCreateWithContext(yourContext,CGSizeMake(1500,1500), NULL); 
CGContextRef notepadContext = CGLayerGetContext(notepad);

// you can for example write an image in to notepad
CGImageRef imageExamp = [[UIImage imageWithContentsOfFile:
    [[NSBundle mainBundle] pathForResource:@"smallTestImage" ofType:@"png"] ] CGImage];
CGContextDrawImage( notepadContext, CGRectMake(100,100, 50,50), imageExamp);

// setting the colorspace may or may not be relevant to you
CGContextSetFillColorSpace( notepadContext, CGColorSpaceCreateDeviceRGB() );

// you can draw to notepad as much as you like in the normal way
// don't forget to push it's context on and off your work space so you can draw to it

UIGraphicsPushContext(notepadContext);

// set the colors
CGContextSetRGBFillColor(notepadContext, 0.15,0.25,0.35, 0.45);
// draw rects
UIRectFill(CGRectMake(x,y,w,h));
// draw ovals, filled stroked or whatever you wish
UIBezierPath* d = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x,y,w,h)];
[d fill];
// draw cubic and other curves
UIBezierPath *longPath;
longPath.lineWidth = 42;
longPath.lineCapStyle = kCGLineCapRound;
longPath.lineJoinStyle = kCGLineJoinRound;
[longPath moveToPoint:p];
[longPath addCurveToPoint:q controlPoint1:r controlPoint2:s];
[longPath addCurveToPoint:a controlPoint1:b controlPoint2:c];
[longPath addCurveToPoint:m controlPoint1:n controlPoint2:o];
[longPath closePath];
[longPath stroke];

UIGraphicsPopContext();

// so now you have a nice CGLayer.

// how to save it to a file?

// you can save it to a file using the amazing KENNY-TM-METHOD !!!

UIGraphicsBeginImageContext( CGLayerGetSize(notepad) );
CGContextRef rr = UIGraphicsGetCurrentContext();
CGContextDrawLayerAtPoint(rr, CGPointZero, notepad);
UIImage* ii = UIGraphicsGetImageFromCurrentImageContext();
NSData* pp = UIImagePNGRepresentation(ii);
[pp writeToFile:@"foo.png" atomically:YES];
UIGraphicsEndImageContext();

// you may prefer to look at it like this:

UIGraphicsBeginImageContext( CGLayerGetSize(notepad) );
CGContextDrawLayerAtPoint(UIGraphicsGetCurrentContext(), CGPointZero, notepad);
[UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext()) writeToFile:@"foo.png" atomically:YES];
UIGraphicsEndImageContext();

// there are three clever steps in the KENNY-TM-METHOD:
// - start a new UIGraphics image context
// - CGContextDrawLayerAtPoint which can, in fact, draw a CGLayer
// - just use the usual UIImagePNGRepresentation to convert to a png

// done!  a miracle

// if you are testing on your mac-simulator, you'll find the file
// simply in the main drive directory
return;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-31 18:35:49

对于iPhone操作系统,应该可以在CGContext上绘制CGLayer,然后将其转换为UIImage,然后将UIImage编码为PNG并保存。

代码语言:javascript
复制
CGSize size = CGLayerGetSize(layer);
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextDrawLayerAtPoint(ctx, CGPointZero, layer);
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
NSData* pngData = UIImagePNGRepresentation(image);
[pngData writeToFile:... atomically:YES];
UIGraphicsEndImageContext();

(未测试)

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4064470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档