拍摄屏幕图像并导出为PDF,JPG,PNG

时间:2013-01-13 12:53:42

标签: ios cocoa-touch

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

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

我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

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

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

根据您的评论进行修改。回复太大而无法发表评论:

我曾经用GL层做了类似的事情。在你的情况下,它应该更容易。如何从视图层获取UIImage将在此处Convert UIView Layer to UIImageHow 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.

答案 1 :(得分: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