How to print UIImage at 5x7 "in printout?

I would like to give my users the opportunity to print my photos in 4x6 and 5x7 sizes. Printing UIImage at 4x6 "is simple, since 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. Any ideas how to do this? Please provide code samples.

Thanks!

+4
source share
1 answer

First we 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; } 

Remember to reconcile the delegate in the .h file:

 @interface yourViewController : UIViewController <UIPrintInteractionControllerDelegate> 

You will receive a list of the exact paper sizes that your printer supports when you click the Print button on AirPrint. Check it out and see what rooms are available.

Then change the CGSize pageSize to one of the registered values. You'll find out if this works when the last NSLog β€œiOS says the best paper size” now gives you the size of the dot you selected, instead of 4x6, the size of Letter or some other size that you don't need.

The reason you need to use size from the paper list is because the UIPrintPaper bestPaperForPageSize method does not seem to do what its name implies. One would think that, based on his name, he would process a close contribution and try to find something like that. 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 be to choose a letter, no matter what size of the dot I would think working for 5x7. Actual dot sizes vary by region and printer.

Only after entering the matching numbers from the paper list can I print the PDF at 5x7.

Good luck

+5
source

Source: https://habr.com/ru/post/1485139/


All Articles