#質問したいこと
語彙力がなくてどう言えばいいのかわからないのですが、写真を保存してからホーム画面へ戻ると他の内容に違う写真が反映されてしまっています。(下記に写真が掲示してあります。)
#コード
swift5
1//追加画面 2import UIKit 3 4class AddViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 5 6 @IBOutlet weak var memoTextView: UITextView! 7 @IBOutlet weak var imageView: UIImageView! 8 @IBOutlet weak var imageButton: UIButton! 9 10 let saveData: UserDefaults = UserDefaults.standard 11 12 //カメラボタンがタップされた時の処理 13 @IBAction func launchCamera(_ sender: UIButton) { 14 let camera = UIImagePickerController.SourceType.camera 15 if UIImagePickerController.isSourceTypeAvailable(camera) { 16 let picker = UIImagePickerController() 17 picker.sourceType = camera 18 picker.delegate = self 19 self.present(picker, animated: true) 20 } 21 imageView.isHidden = false 22 imageButton.isHidden = true 23 } 24 25 //ユーザーが撮影し終わった時の処理 26 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 27 let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage 28 self.imageView.image = image 29 UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) 30 self.dismiss(animated: true) 31 32 UserDefaults.standard.set(image.jpegData(compressionQuality: 0.8), forKey: "MemoImage") 33 saveData.synchronize() 34 } 35 36 override func viewDidLoad() { 37 super.viewDidLoad() 38 39 imageView.isHidden = true 40 } 41 42 @objc func commitButtonTapped() { 43 self.view.endEditing(true) 44 } 45 46 47 48 @IBAction func save(_ sender: Any) { 49 50 let inputText = memoTextView.text 51 let ud = UserDefaults.standard 52 if ud.array(forKey: "memoArray") != nil{ 53 54 var saveMemoArray = ud.array(forKey: "memoArray") as! [String] 55 56 if inputText != ""{ 57 //配列に追加 58 saveMemoArray.append(inputText!) 59 ud.set(saveMemoArray, forKey: "memoArray") 60 }else{ 61 showAlert(title: "何も入力されていません") 62 63 } 64 65 }else{ 66 var newMemoArray = [String]() 67 68 if inputText != ""{ 69 70 newMemoArray.append(inputText!) 71 ud.set(newMemoArray, forKey: "memoArray") 72 }else{ 73 showAlert(title: "何も入力されていません") 74 } 75 } 76 showAlert(title: "保存完了") 77 ud.synchronize() 78 } 79 80 func showAlert(title:String){ 81 let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert) 82 83 alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 84 85 alert.addAction(UIAlertAction(title: "キャンセル", style: .cancel, handler: nil)) 86 87 self.present(alert, animated: true, completion:nil) 88 } 89 90} 91
swift5
1import UIKit 2 3 4class DetailViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 5 6 @IBOutlet weak var memoTextView: UITextView! 7 @IBOutlet weak var imageView: UIImageView! 8 9 var selectedRow:Int! 10 var selectedMemo : String! 11 12 let saveData: UserDefaults = UserDefaults.standard 13 14 //カメラボタンがタップされた時の処理 15 @IBAction func launchCamera(_ sender: UIBarButtonItem) { 16 let camera = UIImagePickerController.SourceType.camera 17 if UIImagePickerController.isSourceTypeAvailable(camera) { 18 let picker = UIImagePickerController() 19 picker.sourceType = camera 20 picker.delegate = self 21 self.present(picker, animated: true) 22 } 23 } 24 25 //ユーザーが撮影し終わった時の処理 26 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 27 28 let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage 29 self.imageView.image = image 30 UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) 31 self.dismiss(animated: true) 32 UserDefaults.standard.set(image, forKey: "MemoImage") 33 saveData.synchronize() 34 } 35 36 37 38 override func viewDidLoad() { 39 super.viewDidLoad() 40 memoTextView.text = selectedMemo 41 let kbToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 40)) 42 kbToolBar.barStyle = UIBarStyle.default // スタイルを設定 43 kbToolBar.sizeToFit() // 画面幅に合わせてサイズを変更 44 // スペーサー 45 let spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil) 46 // 閉じるボタン 47 let commitButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(self.commitButtonTapped)) 48 kbToolBar.items = [spacer, commitButton] 49 memoTextView.inputAccessoryView = kbToolBar 50 51 let imageData:NSData = UserDefaults.standard.object(forKey: "MemoImage") as! NSData 52 imageView.image = UIImage(data: imageData as Data) 53// imageView = saveData.object(forKey: "MemoImage") as! UIImageView 54 } 55 56 @objc func commitButtonTapped() { 57 self.view.endEditing(true) 58 } 59 60 //画面遷移する時にタップするボタン(保存) 61 @IBAction func save(_ sender: Any) { 62 63 let inputText = memoTextView.text 64 let ud = UserDefaults.standard 65 if ud.array(forKey: "memoArray") != nil{ 66 //saveMemoArrayに取得 67 var saveMemoArray = ud.array(forKey: "memoArray") as! [String] 68 //テキストに何か書かれているか? 69 if inputText != ""{ 70 //配列に追加 71 saveMemoArray[selectedRow] = inputText! 72 ud.set(saveMemoArray, forKey: "memoArray") 73 }else{ 74 showAlert(title: "何も入力されていません") 75 76 } 77 78 }else{ 79 //最初、何も書かれていない場合 80 var newMemoArray = [String]() 81 //nilを強制アンラップはエラーが出るから 82 if inputText != ""{ 83 //inputtextはoptional型だから強制アンラップ 84 newMemoArray.append(inputText!) 85 ud.set(newMemoArray, forKey: "memoArray") 86 }else{ 87 showAlert(title: "何も入力されていません") 88 } 89 } 90 showAlert(title: "保存完了") 91 ud.synchronize() 92 } 93 94 func showAlert(title:String){ 95 let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert) 96 97 alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 98 99 alert.addAction(UIAlertAction(title: "キャンセル", style: .cancel, handler: nil)) 100 101 self.present(alert, animated: true, completion:nil) 102 } 103 //削除ボタン 104 @IBAction func deleteMemo(_ sender: Any) { 105 let ud = UserDefaults.standard 106 if ud.array(forKey: "memoArray") != nil{ 107 var saveMemoArray = ud.array(forKey: "memoArray") as![String] 108 saveMemoArray.remove(at: selectedRow) 109 ud.set(saveMemoArray, forKey: "memoArray" ) 110 ud.synchronize() 111 //画面遷移 112 self.navigationController?.popViewController(animated: true) 113 } 114 } 115 116 //シェアボタン 117 @IBAction func showActivityView(_ sender: UIBarButtonItem) { 118 let activitycontroller = UIActivityViewController(activityItems: [memoTextView], applicationActivities: nil) 119 120 self.present(activitycontroller, animated: true, completion: nil) 121 } 122 123} 124
急いでおります。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー