So the storyboard height I have chosen to use to set up my screen is 667 (iPhone 6 height). However, if I were to use a 4s simulator, I am only able to see 480 of that 667 (to view the extra 187 pixels, I have a scrollview set up). With that in mind, would I able to create code to screenshot the entire 667 pixel storyboard, even though I may use a 4s simulator which has a max height of 480 and will never be able to view the whole 667 pixel height?
-
1Yes, just screenshot your scroll view bounds. – Douglas Feb 29 '16 at 23:50
-
Fantastic! Would you happen to know of any tutorials regarding a scrollview screenshot? – jonpeter Mar 01 '16 at 00:15
-
There's this: http://stackoverflow.com/a/18925301/1305067 – paulvs Mar 01 '16 at 00:15
-
I'm trying to make this easy on myself, haha, so please forgive the basic of my question. Would it be possible to edit this code to capture the scrollview image? UIGraphicsBeginImageContext(view.frame.size) view.layer.renderInContext(UIGraphicsGetCurrentContext()!) let source = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() UIImageWriteToSavedPhotosAlbum(source,nil, nil, nil) – jonpeter Mar 01 '16 at 00:17
-
https://m.youtube.com/watch?v=MEm8seoQaxY I used this to start, then searched here on SO under take screenshot programmatically. Sort of put them both together. – Douglas Mar 01 '16 at 00:27
-
If you don't mind me asking, is your app built in obj-c? – jonpeter Mar 01 '16 at 00:37
-
Yes it is. But the code is quickly converted to swift. Also, if you put an at symbol in front of the username, they will get notified you left a comment for them. I was just coming back to see if you got an answer and saw your question. – Douglas Mar 01 '16 at 01:45
-
Let me know if you want me to post some code. In my app I have a large scroll view, and when I take a screen shot to print it, it prints the whole scroll view. It is in Obj-C, but I can see what I can do about Swift. – Douglas Mar 01 '16 at 12:20
-
If you wouldn't mind, that would be incredibly helpful! – jonpeter Mar 01 '16 at 17:32
-
OK, I will, didn't see your comment. At work right now, will post some code in a bit. – Douglas Mar 02 '16 at 12:22
1 Answers
To accomplish this screenshot, I took a screenshot of my entire tableview, turned it into a PDF so I could print it. Hope this helps you out.
First in my .h file I used the UIPrintInteractionControllerDelegate so I could eventually print my screenshot.
Then in my .m file after the interface I declared a variable to store the pdf data.
@property (nonatomic, strong) NSMutableData *pdfData;
Also in my .m file I created a button that would print and hooked it up in IB to the method 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
}
Getting back to the createPDF method. This is what will help you the most.
- (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;
}
So now I have a button that will screen shot the whole table view, in your case your scrollview, turn it into a pdf and print if the device is capable of air printing. To check that I used in the viewDidLoad method....
if ([UIPrintInteractionController isPrintingAvailable])
{
_printButton.enabled = YES;
_printButton.width = 44.0f;
}
else
{
_printButton.enabled = NO;
_printButton.width = 0.0f;
}
This just turns the button off if you can't print. I hope this helps you out. Let me know if you have any questions.

- 2,524
- 3
- 29
- 44
-
I also have another app that just takes a screen shot of the view, but you could take the entire scrollview and save that to photos and prints that as well. Can show you that code too. – Douglas Mar 02 '16 at 14:59
-
Douglas, first off, thank you for the template! Secondly, and I'm probably already aware, would you happen to have this similar function in swift as opposed to Obj-C? – jonpeter Mar 09 '16 at 07:40
-
@jonpeter, I don't know much swift, but I know things will be different. However, if you go to Apples developer page, and look for the items I used, they have that function written in both languages. Like instead of using my (void) method, it would be func method. For the properties you will just declare them in the func viewWillAppear. So for the data for the pdf it would be var pdfData : NSMutableData. Hope this helps you on your way. Good luck. – Douglas Mar 09 '16 at 22:25