2015-09-04 15 views
5

Tôi đang cố gắng thiết lập in qua ứng dụng iPad, trong đó nhấp vào In sẽ in chế độ xem với tất cả nội dung của nó. Đây là những gì tôi đã cố gắng (kéo nhau từ một vài ví dụ trực tuyến):Nội dung AirPrint của UIView

// This is the View I want to print 
// Just a 200x200 blue square 
var testView = UIView(frame: CGRectMake(0, 0, 200, 200)) 
testView.backgroundColor = UIColor.blueColor() 

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 
// This is where I was thinking the print job got the 
// contents to print to the page?? 
printController?.printFormatter = testView.viewPrintFormatter() 

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

Tuy nhiên, tôi cũng đọc here rằng viewPrintFormatter là chỉ dành cho những UIWebView, UITextView, và MKMapView, là đúng?

Khi tôi in bằng cách này (sử dụng trình mô phỏng máy in), tôi chỉ nhận được một trang trống; đã thử với nhiều máy in/khổ giấy khác nhau.

Mọi hướng dẫn đều được đánh giá cao!

+0

Điều gì sẽ xảy ra khi bạn thử? – Undo

+0

Câu hỏi không được cập nhật của tôi –

Trả lời

9

Tôi không chắc đây có phải là cách thích hợp để thực hiện việc này hay không, nhưng tôi đã giải quyết điều này bằng cách chuyển đổi chế độ xem thành UIImage và sau đó đặt nó làm bộ điều khiển in printingItem.

đang Cập nhật:

// This is the View I want to print 
// Just a 200x200 blue square 
var testView = UIView(frame: CGRectMake(0, 0, 200, 200)) 
testView.backgroundColor = UIColor.blueColor() 

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 = testView!.toImage() 

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

Phương pháp toImage() là một phần mở rộng để UIView:

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

     drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true) 

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

Open để tiếp cận thay thế nếu có ai có một!

+0

Rất tuyệt vời, cảm ơn! – LinusGeffarth

+2

Một giải pháp rất tuyệt vời, đã làm việc hoàn hảo cho tôi. Cảm ơn rất nhiều.. –

1

Có thể ai đó (như tôi) cần phải thêm đường viền vào lần xem hình ảnh trước khi gửi cho máy in (nếu không hình ảnh sẽ tự động phù hợp với trang tính). Tôi đã tìm kiếm một số phương pháp dựng sẵn để làm điều này, nhưng tôi chưa tìm thấy nó (btw, tôi đọc một số gợi ý từ here). Bí quyết là thêm chế độ xem chứa hình ảnh vào chế độ xem bên ngoài và sau đó căn giữa nó.

let borderWidth: CGFloat = 100.0 
    let myImage = UIImage(named: "myImage.jpg") 
    let internalPrintView = UIImageView(frame: CGRectMake(0, 0, myImage.size.width, myImage.size.height)) 
    let printView = UIView(frame: CGRectMake(0, 0, myImage.size.width + borderWidth*2, myImage.size.height + borderWidth*2)) 
    internalPrintView.image = myImage 
    internalPrintView.center = CGPointMake(printView.frame.size.width/2, printView.frame.size.height/2) 
    printView.addSubview(internalPrintView) 
    printController.printingItem = printView.toImage() 

Đó là một chút phức tạp nhưng nó thực hiện công việc dơ bẩn của nó.

Các vấn đề liên quan