您可以调整传递给 UIPrintInteractionController.PrintingItems 的图像的大小吗?

Can you resize images that are passed to UIPrintInteractionController.PrintingItems?

我正在尝试使用 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");
            }
        });
    }

有几种方法可以完成您的要求,这里只是使用图形上下文进行打印页面渲染的一种方法。

  • 将您的图像加载到 UIImage 实例中
    • 您可能想要 pre-scale/re-scale 该图像
  • 创建一个足够大的 UIView 以包含该图像的所有行和列
  • 为您的 "printed page" 上的每个 row/column 创建一个包含您的图像的 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");
    }
});

注:!!!这不是在处理清理工作,请确保在完成后 Dispose 一切...

注意:这只是一个简单示例,并未处理图像与纸张尺寸的纵横比,您需要根据打印的内容和类型 printer/paper 来考虑这一点。 ..

注意:如果您渲染的是多个文本字段而不是图像,您可能希望渲染成 PDF 而不是 UIImage 并打印它。