【问题标题】:Take a screenshot of screen截取屏幕截图
【发布时间】:2012-03-22 17:56:29
【问题描述】:

我需要截取屏幕截图并保存为 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);
}

【问题讨论】:

  • 这是您自己问题的扩展副本吗? stackoverflow.com/questions/9724378/…
  • 好吧,但我在完成任务时遇到了问题。以上是我从上一个问题中得到的。但下面的答案是正确的,这解决了问题
  • 好的,我看到你成功了。这很好!

标签: iphone objective-c ios xcode ios4


【解决方案1】:

被注释掉的这一行应该是把你的图片写到 pdf 中。把那个代码放回去,它可能会工作。

[aView.layer renderInContext:UIGraphicsGetCurrentContext()];

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

【讨论】:

    猜你喜欢
    • 2014-09-26
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 2021-11-14
    相关资源
    最近更新 更多