2015-12-10 21 views
9

Tôi đang phát triển một ứng dụng yêu cầu khách truy cập phải được tạo và in trực tiếp từ iPad qua AirPrint.In chế độ xem trong iOS bằng Swift

Tôi đã xem ở khắp mọi nơi để tìm hiểu cách in chế độ xem nhưng tôi chỉ có thể tìm cách in văn bản, webKit và mapKit.

Có cách nào để in toàn bộ chế độ xem không? Nếu không, những gì sẽ là một giải pháp tốt để in một khách truy cập vượt qua đó sẽ được văn bản đơn giản, hộp và một bức ảnh. Cảm ơn.

+0

Xin lỗi ipad1 cũ của tôi không cho tôi thêm một bình luận duy nhất một câu trả lời. Nhận xét của tôi là: Didvyou tìm ra giải pháp? Tôi đang tìm kiếm cùng cũng với một mã qr? Lúc đầu, tôi thích sử dụng ứng dụng ipad của tôi giống như thiết bị đầu vào và có một mạng máy tính cục bộ tra cứu dữ liệu và in huy hiệu/vượt qua? Tôi nghĩ rằng một cách nhanh hơn để có bố trí in được tạo ra trong ứng dụng của tôi dựa trên dữ liệu cơ sở dữ liệu? Có thể tạo ra một pass/huy hiệu pdf với một qrcode và in này? – alex

Trả lời

6

Tôi đã tìm thấy câu trả lời cho câu hỏi của tôi bằng cách sửa đổi mã tìm thấy ở đây: AirPrint contents of a UIView

//create an extension to covert the view to an image 
extension UIView { 
func toImage() -> UIImage { 
    UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale) 

    drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true) 

    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image 
} 
} 

//In your view controller  
@IBAction func printButton(sender: AnyObject) { 

    let printInfo = UIPrintInfo(dictionary:nil) 
    printInfo.outputType = UIPrintInfoOutputType.General 
    printInfo.jobName = "My Print Job" 

    // Set up print controller 
    let printController = UIPrintInteractionController.sharedPrintController() 
    printController.printInfo = printInfo 

    // Assign a UIImage version of my UIView as a printing iten 
    printController.printingItem = self.view.toImage() 

    // Do it 
    printController.presentFromRect(self.view.frame, inView: self.view, animated: true, completionHandler: nil) 
} 
3

Tôi nghĩ rằng bạn phải nhìn in ảnh mẫu mã với Swift: https://developer.apple.com/library/ios/samplecode/PrintPhoto/Introduction/Intro.html

gì chính xác là xem, IMAGExem hoặc UIView của bạn? Nếu bạn quan tâm đến imageView hoặc UIImage, mẫu In Ảnh từ Apple là dành cho bạn. Nếu chủ đề của bạn là UIView bạn có thể tạo ngữ cảnh pdf từ view.layers và gửi đến FP của AirPrint như WebKit, văn bản hoặc bạn có thể in để tạo dữ liệu pdf.

Giải pháp tốt nhất là tạo tập tin PDF là ở đây để nhanh chóng tập tin pdf Generate PDF with Swift

In là để thực hiện nhanh chóng:

var pdfLoc = NSURL(fileURLWithPath:yourPdfFilePath) 
let printController = UIPrintInteractionController.sharedPrintController()! 
let printInfo = UIPrintInfo(dictionary:nil)! 

printInfo.outputType = UIPrintInfoOutputType.General 
printInfo.jobName = "print Job" 
printController.printInfo = printInfo 
printController.printingItem = pdfLoc 
printController.presentFromBarButtonItem(printButton, animated: true, completionHandler: nil) 
+2

Nếu câu hỏi này được gắn thẻ với [tag: swift], tại sao bạn lại cung cấp một thực thi Mục tiêu-C? – JAL

+0

Bởi vì tôi không quen thuộc với rất nhanh, tôi đã chỉnh sửa câu trả lời của mình. – furkan

+0

Cảm ơn, nó có thể sẽ là một UIView. Tôi đã xem xét các giải pháp ở trên nhưng tôi không chắc chắn tùy chọn PDF là đặt cược tốt nhất. Tôi nghĩ điều tốt nhất để làm là tạo HTML khách truy cập vượt qua rồi in chế độ xem web. Tôi sẽ cho bạn biết làm thế nào tôi nhận được trên. –

1

Hier trong 3.x Swift

func prt() { 

     let printInfo = UIPrintInfo(dictionary:nil) 
     printInfo.outputType = UIPrintInfoOutputType.general 
     printInfo.jobName = "My Print Job" 

     // Set up print controller 
     let printController = UIPrintInteractionController.shared 
     printController.printInfo = printInfo 

     // Assign a UIImage version of my UIView as a printing iten 
     printController.printingItem = self.view.toImage() 

     // Do it 
     printController.present(from: self.view.frame, in: self.view, animated: true, completionHandler: nil) 

    } 

}

extension UIView { 
    func toImage() -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale) 

     drawHierarchy(in: self.bounds, afterScreenUpdates: true) 

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image! 
    } 
} 
Các vấn đề liên quan