因此我选择用于设置屏幕的故事板高度为667(iPhone 6高度)。但是,如果我要使用4s模拟器,我只能看到667中的480个(要查看额外的187个像素,我设置了一个scrollview)。考虑到这一点,我是否能够创建代码来截取整个667像素的故事板,即使我可能使用最大高度为480且永远无法查看整个667像素高度的4s模拟器?
答案 0 :(得分:0)
要完成此屏幕截图,我拍摄了整个tableview的屏幕截图,将其转换为PDF,以便我可以打印它。希望这会帮助你。
首先在我的.h文件中,我使用了UIPrintInteractionControllerDelegate,所以我最终可以打印我的截图。
然后在接口后我的.m文件中声明了一个存储pdf数据的变量。
@property (nonatomic, strong) NSMutableData *pdfData;
同样在我的.m文件中,我创建了一个按钮,可以在IB中打印并将其连接到printThis方法。
- (IBAction)printThis:(id)sender
{
[self createPDF]; //Will show you this in a second
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [NSString stringWithFormat:@"ELEMENTS"];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = _pdfData;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error)
{
if (!completed && error)
{
NSLog(@"Printing could not complete because of error: %@", error);
}
};
// This is for iPad
[pic presentFromBarButtonItem:self.printButton animated:YES completionHandler:completionHandler];
// This is for iPhone/iPod
[pic presentAnimated:YES completionHandler:completionHandler];
//You can use an if statement to pick which device you are on, I only used the phone for this app
}
返回createPDF方法。这对你最有帮助。
- (void)createPDF
{
_pdfData = [NSMutableData data];
UITableView *tempView = [[UITableView alloc] init]; //I printed a tableview, you can print your scroll view
tempView = _elementTableView; //Put your scroll view in the temp view
_headerView.hidden = YES; //I didn't want to print my header but you won't have one
CGRect priorBounds = tempView.bounds; //getting bounds of your scrollview
CGSize fittedSize = [tempView sizeThatFits:CGSizeMake(priorBounds.size.width, HUGE_VALF)];
tempView.bounds = CGRectMake(0, 0, fittedSize.width, fittedSize.height);
CGRect pdfPageBounds = CGRectMake(0, 10, 612, 792); //This is the size of a page of paper so I could print to regular paper
UIGraphicsBeginPDFContextToData(_pdfData, pdfPageBounds, nil);
{
for (CGFloat pageOriginY = 0; pageOriginY < fittedSize.height; pageOriginY += pdfPageBounds.size.height) //This lets me print multiple pages if it goes over
{
UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil);
CGContextSaveGState(UIGraphicsGetCurrentContext());
{
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY);
[tempView.layer renderInContext:UIGraphicsGetCurrentContext()];
}CGContextRestoreGState(UIGraphicsGetCurrentContext());
}
}UIGraphicsEndPDFContext();
tempView.bounds = priorBounds;
}
所以现在我有一个按钮,可以屏幕拍摄整个桌子视图,在你的情况下你的滚动视图,把它变成一个pdf并打印,如果该设备能够进行空气打印。检查我在viewDidLoad方法中使用过....
if ([UIPrintInteractionController isPrintingAvailable])
{
_printButton.enabled = YES;
_printButton.width = 44.0f;
}
else
{
_printButton.enabled = NO;
_printButton.width = 0.0f;
}
如果您无法打印,这只会关闭按钮。我希望这能够帮到你。如果您有任何疑问,请告诉我。