こんにちは!
よろしく御願い致します。
写真をiphoneから選択し、imageviewに貼り付けるのを作ったのですが、Tap Gesture Recognizer をつけているのにもかかわらず写真を貼り付けることができません。間違えている部分などがありましたら訂正していただけませんか。
お手数をおかけしますが、よろしく御願い致します。
コードが多くなりすいません。
swift
1@IBAction func ImageViewTap(_ sender: Any) { 2 3 let genereter = UINotificationFeedbackGenerator() 4 genereter.notificationOccurred(.success) 5 6 //アラートを出す 7 8 //カメラとアルバムを選択させる 9 showAlert() 10 11 } 12 13 //カメラを立ち上げる 14 15 func doCamera(){ 16 let sourceType:UIImagePickerController.SourceType = .camera 17 18 //カメラが利用可能かチェック 19 if UIImagePickerController.isSourceTypeAvailable(.camera) { 20 21 let cameraPicker = UIImagePickerController() 22 cameraPicker.allowsEditing = true 23 cameraPicker.sourceType = sourceType 24 cameraPicker.delegate = self 25 self.present(cameraPicker, animated: true, completion: nil) 26 27 } 28 29 30 } 31 32 func doAlbum(){ 33 34 let sourceType:UIImagePickerController.SourceType = .photoLibrary 35 36 //カメラが利用可能かチェック 37 if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { 38 39 let cameraPicker = UIImagePickerController() 40 cameraPicker.allowsEditing = true 41 cameraPicker.sourceType = sourceType 42 cameraPicker.delegate = self 43 self.present(cameraPicker, animated: true, completion: nil) 44 45 } 46 47 } 48 49 50 51 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 52 53 if info[.originalImage] as? UIImage != nil { 54 55 let selectedImage = info[.originalImage] as! UIImage 56 UserDefaults.standard.set(selectedImage.jpegData(compressionQuality: 0.1), forKey: "userimage") 57 58 LogoImageView.image = selectedImage 59 picker.dismiss(animated: true, completion: nil) 60 61 62 } 63 64 65 } 66 67 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 68 69 dismiss(animated: true, completion: nil) 70 71 } 72 73 74 //アラート 75 76 func showAlert(){ 77 78 let alertController = UIAlertController(title: "選択", message: "どちらを使用しますか?", preferredStyle: .actionSheet) 79 80 let action1 = UIAlertAction(title: "カメラ", style: .default) { (alert) in 81 82 self.doCamera() 83 84 } 85 86 let action2 = UIAlertAction(title: "アルバム", style: .default) { (alert) in 87 88 self.doAlbum() 89 90 } 91 92 let action3 = UIAlertAction(title: "キャンセル", style: .cancel ) 93 94 alertController.addAction(action1) 95 alertController.addAction(action2) 96 alertController.addAction(action3) 97 self.present(alertController, animated: true, completion: nil) 98 99 }
Storyboard 上で UIImageView に Tap Gesture Recognizer を設定したのに @IBAction が呼ばれないということでしょうか? UIImageView の User Interaction Enabled を on にする必要があるのですが、Storyboard で設定しても無視される場合があるようで、Swift コードで imageView.isUserInteractionEnabled = true とする必要があるかもです。
https://stackoverflow.com/questions/29202882/how-do-you-make-an-uiimageview-on-the-storyboard-clickable-swift
hositakanori様
返信遅くなりまことに申し訳ございません。
ご回答ありがとうございました。
UIImageView の User Interaction Enabled を onにしたら通りました。ありがとうございます。
回答1件
あなたの回答
tips
プレビュー