2016-02-12 1 views
0

Я пытаюсь напечатать некоторый текст из UITextView вместе с одним изображением из UIImageView. Я искал какой-то учебник в Интернете, и мне удалось распечатать текст из UITextView, но я еще не нашел способ распечатать изображение с ним. UIImageView Я хочу напечатать qrImageView, а UITextView называется qrNoteTextView. Я использую следующий код для печати TextView:Objective-C AirPrint UIImageView image

- (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]; 

} 

Спасибо за ваше время!

+0

Преобразование содержимого UITextView в NSData и попытка его печати после кодирования его в формате UTF8. Пусть это поможет вам. – Madhav

ответ

0

Преобразование содержимого UITextView в NSData и попытка его печати после кодирования его в формате UTF8. Пусть это поможет вам.

Или вы можете преобразовать его в Base64 для печати. Вы можете использовать следующий код для печати текста и изображения.

NSMutableString * msgBody = [[NSMutableString alloc] initWithString: @ ""]; [msgBody appendString: [[NSString stringWithFormat: @ "% @", @ "Отпечатано из 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]; 
+0

Я преобразовал UITextView и UIImageView в NSData и объединил их в NSMutableData. Но как я могу напечатать эту NSMutableData? – user3638160

+0

Вы можете использовать следующий код для добавления изображения на UITextView 'UIImage * onions = [self thumbnailOfImageWithName: @ "onion" extension: @ "jpg"]; NSTextAttachment * onionatt = [NSTextAttachment new]; onionatt.image = лук; onionatt.bounds = CGRectMake (0, -5, onions.size.width, onions.size.height); NSAttributedString * onionattchar = [NSAttributedString attribittedStringWithAttachment: onionatt]; NSRange r = [[mas string] rangeOfString: @ "Лук"]; [mas insertAttributedString: onionattchar atIndex: (r.location + r.length)]; self.tv.attributedText = mas; – Madhav

+0

Я добавил код принтера для печати текста и изображения. если его работа, пожалуйста, ответьте. – Madhav

 Смежные вопросы

  • Нет связанных вопросов^_^