[英]Taking on screen image and exporting to PDF, JPG, PNG
我想拍摄iPad屏幕上的图像并将其导出为JPG,PNG或PDF文件。
简单来说,用户在屏幕上创建了一些内容,例如,他点击“导出到PNG”,然后创建一个PNG文件。
我该怎么做呢?
如果它是你正在谈论的UIImage - 它确实有内置方法将其内容写入文件并将图像作为png或jpeg数据流获取。 有关详细信息,请参阅UIImage类参考。
AFAIK没有用于导出为PDF的bild-in功能。 您当然可以创建PDF并将图像绘制到其中。 这个教程很容易理解,它确实包括绘制图像: http : //www.ioslearner.com/generate-pdf-programmatically-iphoneipad/
根据您的评论进行修改。 对于评论,回复太大了:
我曾经用GL层做过类似的事情。 在你的情况下,它应该更容易。 如何从视图的层获取UIImage在这里讨论转换UIView层到UIImage和这里如何将UIView转换为UIImage? 。 一旦你将它存储在UIImage中,请参阅http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/Reference/Reference.html 。 它告诉你有UIKit函数可用(不是我之前说过的方法)将UIImages转换为JPEG和PNG: UIImagePNGRepresentation
和UIImageJPEGRepresentation
,这些都记录在这里: 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.
我为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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.