What's the correct code to save a CGLayer as a PNG file?

前端 未结 1 1430
别那么骄傲
别那么骄傲 2020-12-18 03:35

Please note that this question is about CGLayer (which you typically use to draw offscreen), it is not about CALayer.

In iOS, what\'s the correct co

相关标签:
1条回答
  • 2020-12-18 04:02

    For iPhone OS, it should be possible to draw a CGLayer on a CGContext and then convert into a UIImage, which can then be encoded into PNG and saved.

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

    (not tested)

    0 讨论(0)
提交回复
热议问题