【问题标题】:Objective-C AirPrint UIImageView imageObjective-C AirPrint UIImageView 图像
【发布时间】:2016-02-12 14:31:13
【问题描述】:

我正在尝试从 UITextView 打印一些文本,以及从 UIImageView 打印一张图片。我在网上搜索了一些教程,我成功地从 UITextView 打印了文本,但我还没有找到用它打印图像的方法。 我要打印的 UIImageView 是 qrImageView 而 UITextView 叫做 qrNoteTextView。 我使用以下代码打印 TextView:

- (IBAction)qrPrint:(id)sender {
    NSLog(@"print action");
        [self.qrNoteTextView resignFirstResponder];
        NSMutableString *printBody = [NSMutableString stringWithFormat:@"%@ \n %@", self.qrNoteTextView.text, self.qrImageView.image];

        [printBody appendFormat:@"\nCreated using ..."];

        UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
        pic.delegate = self;

        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
        printInfo.outputType = UIPrintInfoOutputGeneral;
        printInfo.jobName = self.qrNoteTextView.text;
        pic.printInfo = printInfo;

        UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody];
        textFormatter.startPage = 0;
        textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
        textFormatter.maximumContentWidth = 6 * 72.0;
        pic.printFormatter = textFormatter;
        pic.showsPageRange = YES;

        void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
        ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
            if (!completed && error) {
                NSLog(@"Printing could not complete because of error: %@", error);
            }
        };

        [pic presentFromBarButtonItem:self.qrNoteTextView animated:YES completionHandler:completionHandler];

}

感谢您的宝贵时间!

【问题讨论】:

  • 将 UITextView 内容转换为 NSData 并尝试使用 UTF8 格式编码后打印。愿它对你有所帮助。

标签: ios objective-c uiimageview uiimage airprint


【解决方案1】:

将 UITextView 内容转换为 NSData 并尝试使用 UTF8 格式对其进行编码后打印。希望对你有帮助。

或者您可以将其转换为 Base64 以进行打印。您可以使用以下代码打印文本和图像。

NSMutableString *msgBody = [[NSMutableString alloc] initWithString:@""]; [msgBody appendString:[[NSString stringWithFormat:@"%@", @"Printed from MyApp"] stringByReplacingOccurrencesOfString:@"\n" withString:@""]];

                       //Embedding the image
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [NSString stringWithFormat:@"%@/Private Documents", [paths objectAtIndex:0]];

                       NSArray *arrayOfSignatureTitles = [[NSArray alloc] initWithObjects:@"image0.png", @"image1.png", @"image2.png", @"image3.png", nil];

                       for (NSString *imageName in arrayOfSignatureTitles)
{
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    NSData *imageData = [[NSData alloc] initWithContentsOfFile:appFile];

    if (imageData != nil)
    {
        [Base64 initialize];
        NSString *base64String = [Base64 encode:imageData];
        [msgBody appendString:[NSString stringWithFormat:@"  ",base64String]];
    }
}
                       [msgBody appendString:@""];

                       UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
                       pic.delegate = self;

                       UIPrintInfo *printInfo = [UIPrintInfo printInfo];
                       printInfo.outputType = UIPrintInfoOutputGeneral;
                       printInfo.jobName = @"NarrativePro";
                       pic.printInfo = printInfo;

                       UIMarkupTextPrintFormatter *textFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:msgBody];
                       textFormatter.startPage = 0;
                       textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
                       textFormatter.maximumContentWidth = 6 * 72.0;
                       pic.printFormatter = textFormatter;
                       pic.showsPageRange = YES;

                       void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error)
{
    if (!completed && error)
    {
        NSLog(@"Printing could not complete because of error: %@", error);
    }
};

                       [pic presentAnimated:YES completionHandler:completionHandler];

【讨论】:

  • 我将 UITextView 和 UIImageView 转换为 NSData 并将它们放在一个 NSMutableData 中。但是如何打印这个 NSMutableData?
  • 您可以使用以下代码在 UITextView 'UIImage* onions = [self thumbnailOfImageWithName:@"onion" extension:@"jpg"] 上添加图像; NSTextAttachment* onionatt = [NSTextAttachment 新]; onionatt.image = 洋葱; onionatt.bounds = CGRectMake(0,-5,onions.size.width,onions.size.height); NSAttributedString* onionattchar = [NSAttributedString attributesStringWithAttachment:onionatt]; NSRange r = [[mas string] rangeOfString:@"Onions"]; [mas insertAttributedString:onionattchar atIndex:(r.location + r.length)]; self.tv.attributedText = mas;
  • 我为文本和图像打印添加了打印机代码。如果它的工作,请但回答。
  • 您需要下载 Base64 文件,然后将这些文件添加到您的项目代码中。在方法中使用此代码。
  • 当我尝试将代码放入其中时遇到很多错误。它无法识别“mas”、“tv”和“thumbnailOfImagWithName”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
  • 2016-11-14
  • 2013-02-07
  • 1970-01-01
  • 2011-04-23
  • 2011-05-16
  • 2017-04-16
相关资源
最近更新 更多