2010-10-31 7 views
6

Bitte beachten Sie, dass es sich bei dieser Frage um CGLayer handelt (die Sie normalerweise zum Zeichnen außerhalb des Bildschirms verwenden), es geht nicht um CALayer.Wie lautet der korrekte Code zum Speichern eines CGLayers als PNG-Datei?

In iOS, was ist der richtige Code, um eine CGLayer als PNG-Datei zu speichern? Vielen Dank!

Noch einmal, das ist CGLayer, nicht CALayer.

Beachten Sie, dass KANN NICHT Verwendung UIGraphicsGetImageFromCurrentImageContext.

(Aus der Dokumentation: „Sie nennen UIGraphicsGetImageFromCurrentImageContext nur, wenn eine Bitmap-Grafik Kontext der aktuellen Grafikkontext ist.“)

Beachten Sie, dass KANN NICHT Verwendung renderInContext :. renderInContext: ist ausschließlich für CALayers. CGLayers sind total verschieden.

Also, wie können Sie tatsächlich ein CGLayer in ein PNG-Bild konvertieren? Oder in der Tat, wie man einen CGLayer in eine Bitmap in irgendeiner Weise rendert (natürlich kann man dann einfach als Bild speichern).


Später ... Ken hat diese schwierige Frage beantwortet. Ich werde einen langen Beispielcode einfügen, der den Leuten helfen kann. Danke nochmal Ken! Tolle!

-(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; 
} 

Antwort

9

für iPhone OS, sollte es möglich sein, eine CGLayer auf einem CGContext zu zeichnen und dann eine UIImage umwandeln in, die dann in PNG codiert und gespeichert werden können.

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(); 

(nicht getestet)

+0

@ Joe: Siehe Update. – kennytm

+0

Ken, das ist unglaublich - du hast es aus dem Wasser geweht. Gute Arbeit. Sie müssen sehr selbstgefällig sein :) Super .. Vielen Dank nochmal – Fattie

+0

gepriesen! Vielen Dank – Fattie

Verwandte Themen