Reputation: 1840
I would like to give my users the option to print their photos at 4x6 as well as 5x7. Printing a UIImage at 4x6" is easy, as all I have to do is
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputPhoto;
UIPrintInteractionController *printInteractionController = [UIPrintInteractionController sharedPrintController];
printInteractionController.printInfo = printInfo;
printInteractionController.printingItem = self.myImage;
However, I need to print my UIImage at 5x7 as well. Any ideas on how to do this? Please provide code samples.
Thanks!
Upvotes: 4
Views: 2000
Reputation: 801
First, implement this method:
- (UIPrintPaper *) printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray *)paperList {
// This will give us all the available paper sizes from the printer
for(int i = 0; i < paperList.count; i++) {
UIPrintPaper* paper = [paperList objectAtIndex:i];
NSLog(@"List paper size %f,%f",paper.paperSize.width, paper.paperSize.height);
}
// You're going to use precise numbers from the NSLog list here
CGSize pageSize = CGSizeMake(360, 504);
// Give our CGSize to the bestPaperForPageSzie method to try to get the right paper
UIPrintPaper* paper = [UIPrintPaper bestPaperForPageSize:pageSize withPapersFromArray:paperList];
// See if we got the right paper size back
NSLog(@"iOS says best paper size is %f,%f",paper.paperSize.width, paper.paperSize.height);
return paper;
}
Don't forget to conform to the delegate in the .h file:
@interface yourViewController : UIViewController <UIPrintInteractionControllerDelegate>
You will get a list of the exact paper sizes your printer supports when you press the "Print" button on AirPrint. Test it and see what numbers are available.
Then, change the CGSize pageSize to one of the logged values. You will know if it works when the final NSLog "iOS says best paper size is " now gives you the point size you selected instead of a 4x6, Letter size or some other size you don't want.
The reason you need to use a size from the paper list is that the UIPrintPaper bestPaperForPageSize method doesn't seem to do what its name implies. One would think that based on its name, it would handle a close input and try to find something similar. However, it only returns the correct paper size when you give it an explicit match from the list of supported paper sizes.
My problem was that it would always seem to choose Letter no matter what point size I thought would work for a 5x7. The actual point sizes vary across regions and printers.
Only after putting in matching numbers from the paper list can I print my PDF on a 5x7.
Good luck!
Upvotes: 6