【问题标题】:Taking on screen image and exporting to PDF, JPG, PNG获取屏幕图像并导出为 PDF、JPG、PNG
【发布时间】:2013-01-13 12:53:42
【问题描述】:

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

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

我该怎么做呢?

【问题讨论】:

  • 您的意思是屏幕截图还是单个视图?
  • 究竟是什么?视图层或 UIImage 还是什么?
  • 这是一个 UIView,上面有几个 UIScrollViews,其中包含 UIImages。因此,我需要以某种方式以编程方式拍摄“屏幕截图”并以 PNG、JPG 或 PDF 格式创建该图像文件。

标签: ios cocoa-touch


【解决方案1】:

我为 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

【讨论】:

  • 获得 UIImage 对象后,您可以调用 UIImageJPEGRepresentationUIImagePNGRepresentation 来获取原始图像数据,然后将该 NSData 对象用于您想要的任何目的。
  • 嗨,Guy Kogus。感谢提供的代码。我还是 iOS 开发的新手,所以我需要一些时间来理解你的例子。是否有可能在某个地方提供有关如何执行此操作的视频教程?
  • 我想这是一个不错的选择。即使您是 iOS 新手,它也非常先进。您很可能也是 Objective-c 的新手。如果是这样,开始需要一些解释。在 Objective-C 中,您总是可以扩展一个类并添加新方法。这就是这里正在发生的事情。他扩展了 UIView 并添加了两个方法。之后,您可以调用它们类似于任何 UIView 对象的任何原始方法,包括子类。
  • 赞成这种好方法。您能否解释一下为什么将renderImageFromLayer 设计为类方法。将其作为内部引用 self.layer 的实例方法而不是接收层作为参数不是更方便吗?
  • 所以,我应该将此代码放在 UIViewController 的 .h 和 .m 文件中,并根据我的需要对其进行修改以使其正常工作,对吗?还是我做错了什么?
【解决方案2】:

如果它是您正在谈论的 UIImage - 它确实具有用于将其内容写入文件并将图像作为 png 或 jpeg 数据流获取的内置方法。有关详细信息,请参阅 UIImage 类参考。

AFAIK 没有用于导出为 PDF 的 bild-in 功能。您当然可以创建 PDF 并将图像绘制到其中。这个教程很容易理解,它确实包括绘制图像:http://www.ioslearner.com/generate-pdf-programmatically-iphoneipad/

根据您的评论进行编辑。回复太大,无法评论:

我曾经对 GL 层做过类似的事情。在你的情况下,它应该更容易。如何从视图层获取 UIImage 在此处讨论 Convert UIView Layer to UIImage 和此处 How to convert UIView as UIImage? 。将其存储在 UIImage 中后,请在此处查看其参考 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/Reference/Reference.html 。它告诉您有可用的 UIKit 函数(不是我之前所说的方法)将 UIImages 转换为 JPEG 和 PNG:UIImagePNGRepresentationUIImageJPEGRepresentation 记录在这里:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImagePNGRepresentation。这些函数返回 NSData 对象。

NSData 记录在这里:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html 它带有将数据写入文件的方法。 writeToFile:atomically: 例如。

这是一个例子。就我而言,我只需要一个唯一的文件名并决定采用时间戳。您可能希望在此处使用一些对用户更友好的文件名。

// Use current time stamp for some unique photo ID
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocuments=[paths objectAtIndex:0];  // this returns the path to the App's document directory
NSString *photoID = [NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]];  // this is just a time stamp. This is what you are most likely to do different. 
NSString *fileName = [NSString stringWithFormat:@"%@/%@.jpg", pathToDocuments, photoID]; // now you have a fully qualified path to a file within that part of the file system that belongs to your app. It will be overwritten if it exists. 

// Actually save the image (JPEG)
NSData *imgData = UIImageJPEGRepresentation(theImage, 1); // convert to jpeg - 1.0 represents 100%
[imgData writeToFile:fileName atomically:YES]; // actually save the data.

【讨论】:

  • 感谢 PDF 教程,我绝对可以使用它。现在如何处理 JPG 和 PNG?
  • 哇,信息量太大了。我没想到会这么复杂。无论如何,感谢您提供的信息,如果您有代码,我很乐意看一下代码 sn-p。我对 iOS 开发还很陌生,但我认为这个功能总是很方便
猜你喜欢
  • 2020-06-24
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-01
  • 2022-01-14
  • 2012-09-05
相关资源
最近更新 更多