
在iOS中,将CGLayer保存为PNG文件的正确代码是什么?谢谢!
再说一遍,那是CGLayer,而不是CALayer.
请注意,您不能使用UIGraphicsGetimageFromCurrentimageContext.
(从文档中,“只有当基于位图的图形上下文是当前图形上下文时,才能调用UIGraphicsGetimageFromCurrentimageContext.”)
请注意,您不能使用renderInContext:. renderInContext:严格用于CALayers. CGLayers完全不同.
那么,如何将CGLayer实际转换为PNG图像呢?或者实际上,如何以某种方式将CGLayer渲染到位图中(当然,您可以轻松地将其保存为图像).
后来……肯回答了这个棘手的问题.我将粘贴一个可以帮助人们的长示例代码.再次感谢Ken!惊人!
-(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 notepadCGImageRef 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 youCGContextSetFillcolorSpace( 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 itUIGraphicsPushContext(notepadContext);// set the colorsCGContextSetRGBFillcolor(notepadContext,0.15,0.25,0.35,0.45);// draw rectsUIRectFill(CGRectMake(x,y,w,h));// draw ovals,filled stroked or whatever you wishUIBezIErPath* d = [UIBezIErPath bezIErPathWithovalInRect:CGRectMake(x,h)];[d fill];// draw cubic and other curvesUIBezIErPath *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(),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 directoryreturn;}解决方法 对于iPhone OS,应该可以在CGContext上绘制CGLayer然后转换为UIImage,然后可以将其编码为PNG并保存. CGSize size = CGLayerGetSize(layer);UIGraphicsBeginImageContext(size);CGContextRef ctx = UIGraphicsGetCurrentContext();CGContextDrawLayerAtPoint(ctx,layer);UIImage* image = UIGraphicsGetimageFromCurrentimageContext();NSData* pngData = UIImagePNGRepresentation(image);[pngData writetofile:... atomically:YES];UIGraphicsEndImageContext();
(未测试)
总结以上是内存溢出为你收集整理的iphone – 将CGLayer保存为PNG文件的正确代码是什么?全部内容,希望文章能够帮你解决iphone – 将CGLayer保存为PNG文件的正确代码是什么?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)