#40照片編輯 App|文字/拍立得
傳遞資料到下一頁
Published in
4 min readMay 3, 2021
polaroidViewController(編輯拍立得頁面)
📌畫面載入&按下相簿時,呼叫photoLibrary可以從相簿裡選照片
override func viewDidLoad() {super.viewDidLoad()openLibrary()}func openLibrary(){let controller = UIImagePickerController()controller.sourceType = .photoLibrarycontroller.delegate = selfpresent(controller, animated: true, completion: nil)}
📌定義Function(將所選擇的照片顯示在ImageView)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {let image = info[.originalImage] as? UIImagepolaroidImg.image = imagedismiss(animated: true, completion: nil)}
📌打完按下return收回鍵盤
📌傳遞資料
@IBSegueAction func resultPolaroid(_ coder: NSCoder) -> printViewController? {let polaroid = polaroidImg.imageguard let phototext = phototext.text else { return nil }return printViewController(coder: coder, polaroid: polaroid!, phototext: phototext)}
printViewController(印出照片)
📌接收資料
init?(coder: NSCoder, polaroid: UIImage, phototext: String) {
self.polaroid = polaroid
self.phototext = phototext
super.init(coder: coder)
}
required init?(coder: NSCoder) {
fatalError(“init(coder:) has not been implemented”)
}
📌存取照片
@IBAction func savePolaroid(_ sender: UIButton) {
let renderer = UIGraphicsImageRenderer(size: photoView.bounds.size)
let image = renderer.image(actions: { (context) in
photoView.drawHierarchy(in: photoView.bounds, afterScreenUpdates: true)})let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
present(activityViewController, animated: true, completion: nil)
}
GIF: