大阪市中央区 システムソフトウェア開発会社

営業時間:平日09:15〜18:15
MENU

Swiftで画像を書き出す・・UIPrintInteractionControllerで、直にPrinterへ出すかメールやSNS等へか。

株式会社クローバーフィールドの経営理念
著者:川上洋
公開日:2018/05/12
最終更新日:2018/06/20
カテゴリー:技術情報 雑記
タグ:

こんにちは。川上です。

お勉強研究(暇つぶしの名の下で、隠れMyアプリで、時々、作成しています。
で、作り込んでいると、段々にアプリとして形が作って行きます。

その中で、画像をプリントする処理を、色々、試していると、便利なワザを覚えたりしました。

画像(UIImage)をプリントするには、printInteractionControllerとUIPrintInfo を使うのが定石のようです。

printInteractionControllerで「直にPrinter」に書き出す処理と、同処理で「メールやSNS等」に書き出す処理には、ほとんど同じでした。

これが便利でした。
「直にPrinter」と「メールやSNS等」の処理での共通部分にでは、

// UIPrint 情報
let pinfo = UIPrintInfo(dictionary: nil)
pinfo.outputType = .photo
pinfo.jobName = "print Job"
pinfo.orientation = UIPrintInfoOrientation.portrait
pinfo.duplex = UIPrintInfoDuplex.longEdge
// UIPrint インタラクションコントローラ
let pictlr = UIPrintInteractionController.shared
pictlr.printInfo = pinfo
pictlr.printingItems = [pinfo]
pictlr.printingItems?.append(piecvw.image as Any)

です。その後、「直にPrinter」する処理は、

pictlr.present(animated: true, completionHandler: nil)

「メールやSNS等」へに、カメラロールでの書き出しのアレのメニューが出てたのが

//コピー、Print、メールなどへの組み込みメニューの表示用
 let activityViewController = UIActivityViewController(activityItems:printingItems,
                                                       applicationActivities: nil)
        
 present(activityViewController, animated: true, completion: nil)

でした。この辺りのシーケンス周りは、
マニュアルから記述された「印刷のコード例は、『PrintPhoto』、・・・・」からをパクリしました。(^^;)

UIPrintInteractionControllerからpresent でメニューに遷移させるか、
selfのUIViewControllerからpresent で遷移させるかせた。

後は、デフォルトのメニューでの処理は、お任せだったので、こっちでの処理はなかったので
とっても、便利で楽でしたw!!

ではでは。

    上に戻る