Export CGPath as JPG or PNG

后端 未结 2 542
[愿得一人]
[愿得一人] 2021-01-07 13:19

Is it possible to take a path draw in an UIView with CGPath and export it as a PNG?

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-07 13:37

    Assuming you want a UIImage, not a png file, you can do something like this:

    UIGraphicsBeginImageContext(size); 
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextSetLineWidth(context, 2.0);
    CGContextSetLineCap(context, kCGLineCapSquare);
    
    //DRAW YOUR PATH HERE
    
    CGContextStrokePath(context);
    
    myUIImage = UIGraphicsGetImageFromCurrentImageContext();
    [myUIImage retain];  
    
    UIGraphicsEndImageContext();
    

提交回复
热议问题