iphone - 拍摄屏幕截图

标签 iphone objective-c ios xcode ios4

我需要对屏幕进行截图并另存为pdf。我已经完成了“另存为pdf”任务,但是我所截取的屏幕快照始终提供空白的pdf。我不知道为什么。我的代码如下:

 -(IBAction)takeScreenShot
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *newImage = [[UIImageView alloc] initWithImage:image];
UIGraphicsEndImageContext();

[self createPDFfromUIView:newImage saveToDocumentsWithFileName:@"SecondScreen1.pdf"];
}


-(void)createPDFfromUIView:(UIImageView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
//CGSize pageSize = CGSizeMake(612, 792);
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();

// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
//[aView.layer renderInContext:UIGraphicsGetCurrentContext()];

// remove PDF rendering context
UIGraphicsEndPDFContext();

// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}

最佳答案

注释掉的这一行应将您的图像写入pdf。放回该代码,它可能会起作用。

[aView.layer renderInContext:UIGraphicsGetCurrentContext()];

如果失败(没有错误),请确保UIImage * image不为空。

关于iphone - 拍摄屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9827864/

相关文章:

objective-c - 如何检查一个对象是否被@synchronized

objective-c - 将维基百科页面部分转换为 NSString Objective-C

ios - 如果 "self"是 ViewController,你应该几乎总是使用 [weak self] 吗?

objective-c - 如何实现在一个部分中点击一个 UITableViewCell 导航到多个 UITableViewCells 以获取详细信息?

ios - Cocos2d 中的 CCAnimation 和移动 Sprite 是否正确使用 physicsBody?

iphone - 通过 Objective-C 创建 SQLite3 数据库文件

iphone - iPhone应用程序崩溃,释放后在dealloc中第二次获得坏访问

iphone - 核心运动陀螺仪360度值

ios - iOS 项目上的命令脚本问题

iOS/Obj-C - didReceiveRemoteNotification 语句没有触发?