swiftで今カメラアプリを作っているのですが、参考書やネットなどを読んで作っている中で何故このコードがあるのか?というところがあったため質問させていただきたいです。
swift
1import UIKit 2import Photos 3 4class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate { 5 6 7 @IBOutlet var backImageView: UIImageView! 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 PHPhotoLibrary.requestAuthorization { (status) in 13 14 15 switch(status){ 16 case .authorized: 17 print("Authorize") 18 case .denied: 19 print("deny") 20 case .notDetermined: 21 print("NotDetermined") 22 case .restricted: 23 print("Restricted") 24 @unknown default: 25 fatalError("sorry") 26 } 27 28 } 29 30 31 } 32 33 @IBAction func openCamera(_ sender: Any) { 34 let souceType:UIImagePickerController.SourceType = UIImagePickerController.SourceType.camera 35 36 if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){ 37 38 let cameraPicker = UIImagePickerController() 39 cameraPicker.sourceType = souceType 40 cameraPicker.delegate = self 41 self.present(cameraPicker,animated:true,completion: nil ) 42 } 43 } 44 45 46 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 47 48 let pickedImage = info[.originalImage] as! UIImage 49 50 backImageView.image = pickedImage 51 UIImageWriteToSavedPhotosAlbum(pickedImage, nil, nil, nil) 52 picker.dismiss(animated: true, completion: nil) 53 54 } 55 56 57 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 58 picker.dismiss(animated: true, completion: nil) 59 60 } 61 62 63 64 @IBAction func openAlbum(_ sender: Any) { 65 66 let souceType:UIImagePickerController.SourceType = UIImagePickerController.SourceType.photoLibrary 67 68 if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){ 69 70 let cameraPicker = UIImagePickerController() 71 cameraPicker.sourceType = souceType 72 cameraPicker.delegate = self 73 self.present(cameraPicker,animated:true,completion: nil ) 74 75 76 } 77} 78 79 80 @IBAction func share(_ sender: Any) { 81 82 let text = "" 83 let url = NSURL(string:"http://www.apple.com") 84 85 let items = [text,url,backImageView.image] as [Any] 86 87 let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil) 88 89 self.present(activityVC,animated: true,completion: nil) 90 } 91 92}
上のコードで
swift
1func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 2 3 let pickedImage = info[.originalImage] as! UIImage 4 5 backImageView.image = pickedImage 6 UIImageWriteToSavedPhotosAlbum(pickedImage, nil, nil, nil) 7 picker.dismiss(animated: true, completion: nil) 8 9 }
という箇所と
swift
1func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 2 picker.dismiss(animated: true, completion: nil) 3 4 }
という箇所があり、前者も後者もpicker.dismiss(animated:true,completion:nil)があります。
後者のこのコードを含むところを書く前と、書かない時の処理が全く同じで何故必要なのか?
コード全体を通しても理解できませんでした。
なので何故この後者のコードがあるのかわかる方がもしいらっしゃいましたら是非教えていただきたいです。
上手くまとめられずわかりづらくてすいません。。。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/16 05:06
2019/07/16 05:44
2019/07/16 06:04