你可以调整传递给UIPrintInteractionController.PrintingItems的图像吗?

时间:2017-05-04 16:31:57

标签: ios xamarin xamarin.ios uiprintinteractioncntrler

我正在尝试使用UIPrintInteractionController.PrintingItems打印多个标签但是当加载图像时它会填充页面,是否可以使用此方法在一个页面上显示多个图像,或者是否有另一种方法可以让我更好解?感谢。

  void Print()
    {
        var printInfo = UIPrintInfo.PrintInfo;
        printInfo.OutputType = UIPrintInfoOutputType.General;
        printInfo.JobName = "Label Printing";

        //UIPrintFormatter formatter = new UIPrintFormatter()
        //{
        //    StartPage = 0,
        //    ContentInsets = new UIEdgeInsets(72, 72, 72, 72),
        //    MaximumContentWidth = 6 * 72,

        //};

        int labelAmount = 10;
        int x;
        NSObject[] labelItems = new NSObject[labelAmount];
        for (x = 0; x < labelAmount; x++)
        {
            labelItems[x] = UIImage.FromFile("Images/BMcAzurri.png");
        }

        UIPrintInteractionController printer = UIPrintInteractionController.SharedPrintController;
        printer.PrintInfo = printInfo;
        //printer.PrintFormatter = formatter;
        printer.PrintingItems = labelItems;
        printer.ShowsPageRange = true;
        printer.Present(true, (handler, completed, err) => {
            if (!completed && err != null)
            {
                Console.WriteLine("error");
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

有几种方法可以做你要问的,这里只是一个使用图形上下文来进行打印页面渲染。

  • 将图片加载到SELECT DISTINCT id, eid, date, pid, COUNT(pid) AS NumOccurrences FROM mytable实例中
    • 您可能希望预先缩放/重新缩放该图像
  • 创建一个足够大的UIImage,以包含该图片的所有行和列
  • 创建一个UIView,其中包含您&#34;打印页面上每行/每列的图片&#34;并将其放在此视图中的正确位置
  • 使用图片渲染上下文,将包含图片的UIImageView转换为UIView并排队并根据需要打印多个图片

实施例

UIImage

注意:!!!这不是处理清理工作,请确保在完成后var printInfo = UIPrintInfo.PrintInfo; printInfo.OutputType = UIPrintInfoOutputType.General; printInfo.JobName = "Label Printing"; var uiImage = UIImage.FromFile("BingWallpaper-2017-05-04.jpg"); var noColumns = 2; var noRows = 2; var gapBetweenImages = 25; int noOfPages = 2; NSObject[] labelItems = new NSObject[noOfPages]; for (int x = 0; x < noOfPages; x++) { var aPrintView = new UIView { Frame = new CGRect(0, 0, (uiImage.Size.Width * noColumns) + ((noColumns + 1) * gapBetweenImages), (uiImage.Size.Height) * noRows + ((noRows + 1) * gapBetweenImages)) }; for (int column = 0; column < noColumns; column++) { for (int row = 0; row < noRows; row++) { var printImage = new UIImageView(uiImage) { Frame = new CGRect((column * gapBetweenImages) + (column * uiImage.Size.Width), (row * gapBetweenImages) + (row * uiImage.Size.Height), uiImage.Size.Width, uiImage.Size.Height) }; aPrintView.Add(printImage); } } UIGraphics.BeginImageContext(aPrintView.Bounds.Size); aPrintView.Layer.RenderInContext(UIGraphics.GetCurrentContext()); var aPageImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); labelItems[x] = aPageImage; } UIPrintInteractionController printer = UIPrintInteractionController.SharedPrintController; printer.PrintInfo = printInfo; printer.PrintingItems = labelItems; printer.ShowsPageRange = true; printer.Present(true, (handler, completed, err) => { if (!completed && err != null) { Console.WriteLine("error"); } }); 完成所有事情......

注意:这只是一个快速示例,无法处理图像与纸张尺寸的纵横比,您需要根据打印的内容和打印机/纸张的类型来考虑...

注意:如果您要渲染大量文本字段与图像,则可能需要渲染为PDF与Dispose并打印出来。

enter image description here