gpt4 book ai didi

ios - 获取屏幕图像并导出为 PDF、JPG、PNG

转载 作者:行者123 更新时间:2023-11-28 22:41:51 24 4
gpt4 key购买 nike

我想拍摄 iPad 屏幕上的图像并将其导出为 JPG、PNG 或 PDF 文件。

简单来说,用户在屏幕上创建了一些东西,例如他点击“导出到 PNG”,然后创建了一个 PNG 文件。

我该怎么做?

最佳答案

我为 UIView 编写了这个类别,是在一些不记得的来源的帮助下获得的:

@interface UIView (Rendering)

+ (UIImage *)renderImageFromLayer:(CALayer *)layer withRect:(CGRect)frame;
- (UIImage *)renderImageWithRect:(CGRect)frame;

@end

@implementation UIView (Rendering)

+ (UIImage *)renderImageFromLayer:(CALayer *)layer withRect:(CGRect)frame
{
// Create a new context of the desired size to render the image
UIGraphicsBeginImageContextWithOptions(frame.size, YES, 1.0);
CGContextRef context = UIGraphicsGetCurrentContext();

// Translate it, to the desired position
CGContextTranslateCTM(context, -frame.origin.x, -frame.origin.y);

// Render the view as image
[layer renderInContext:context];

// Fetch the image
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();

// Cleanup
UIGraphicsEndImageContext();

return renderedImage;
}

- (UIImage *)renderImageWithRect:(CGRect)frame
{
return [UIView renderImageFromLayer:self.layer withRect:frame];
}

@end

关于ios - 获取屏幕图像并导出为 PDF、JPG、PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14303669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com