How Do I Save What I have Drawn In A CGContext

后端 未结 4 846
余生分开走
余生分开走 2020-12-30 12:28

I have drawn into a CGContext of a UIView.

- (void)drawRect:(CGRect)rect { 
    [self drawInContext:UIGraphicsGetCurrentContext()]  
}

I wo

相关标签:
4条回答
  • 2020-12-30 12:40
    let imageWidth = inputCGImage.width
    let imageHeight = inputCGImage.height
    
    let imageRect = NSRect(
        origin: .zero,
        size: CGSize(width: imageWidth, height: imageHeight)
    )
    
    context.draw(inputCGImage, in: imageRect)
    
    let outputImageRef = context.makeImage()!
    
    let paths = NSSearchPathForDirectoriesInDomains(.desktopDirectory, .userDomainMask, true) as [String]
    
    let destinationURL = NSURL(fileURLWithPath: paths.first!).appendingPathComponent("output.png")!
    
    guard let destination = CGImageDestinationCreateWithURL(destinationURL as CFURL, kUTTypePNG, 1, nil) else {
        throw HoleFillingError.CoreGraphicsError
    }
    
    CGImageDestinationAddImage(destination, outputImageRef, nil)
    CGImageDestinationFinalize(destination)
    
    0 讨论(0)
  • 2020-12-30 12:44
    CGImageRef imgRef = CGBitmapContextCreateImage(context);
    
    UIImage* img = [UIImage imageWithCGImage:imgRef];
    
    CGImageRelease(imgRef);
    
    0 讨论(0)
  • 2020-12-30 12:46
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    NSData *imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:@"image.png"];
    
    0 讨论(0)
  • Call UIGraphicsGetImageFromCurrentImageContext to get an UIImage.
    Then call UIImagePNGRepresentation to get an NSData of the UIImage encoded in PNG.
    Finally, call -writeToFile:… to save the NSData.

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