Swiftでライブラリの画像を選択したらその画像が複製される問題を解決したい
複製されないようにするにはどうしたらいいのか?を知りたい
Swift
1import UIKit 2 3//画像を選択 4extension TapViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { 5 6 //画像を選んだ時の処理 7 func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 8 9 let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage 10 11 self.imageView.image = image 12 UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) 13 //サイズを圧縮する 14 // let resizedImage = selectedImage.scale(byFactor: 0.4) 15 16 image = selectedImage 17 18 var imageHeight = image.size.height 19 var imageWidth = image.size.width 20 21 let navigationBarHeight = navigationController?.navigationBar.frame.height 22 let width = self.view.frame.width 23 let height = self.view.frame.height 24 let centerX = self.view.center.x 25 let centerY = self.view.center.y 26 let widthRatio = imageWidth 27 let heightRatio = imageHeight 28 //画像の大きさに応じてiamgeviewのサイズを変える 29 if imageHeight > self.view.frame.height || imageWidth > self.view.frame.width { 30 print("1") 31 imageWidth = width 32 imageHeight = width*heightRatio/widthRatio 33 34 } else if imageHeight > self.view.frame.height { 35 print("2") 36 imageHeight = height 37 imageWidth = height*widthRatio/heightRatio 38 39 } else if imageWidth > self.view.frame.width { 40 print("3") 41 imageWidth = width 42 imageHeight = width*heightRatio/widthRatio 43 44 } else { 45 } 46 47 imageView.contentMode = UIView.ContentMode.scaleToFill 48 imageView.frame.size = CGSize(width: imageWidth, height: imageHeight) 49 //画像がnavigationbarに被らないようにする 50 if imageHeight/2 > (height/2 - navigationBarHeight!) { 51 print("4") 52 imageView.center = CGPoint(x: centerX, y: centerY + navigationBarHeight!) 53 } else { 54 print("5") 55 imageView.center = CGPoint(x: centerX, y: centerY) 56 } 57 58 imageView.image = image 59 60 picker.dismiss(animated: true, completion: nil) 61 } 62 63 64 65 // 撮影がキャンセルされた時に呼ばれる 66 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 67 picker.dismiss(animated: true, completion: nil) 68 } 69 70 func tappedlibrary() { 71 let sourceType:UIImagePickerController.SourceType = UIImagePickerController.SourceType.photoLibrary 72 73 if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){ 74 // インスタンスの作成 75 let cameraPicker = UIImagePickerController() 76 cameraPicker.sourceType = sourceType 77 cameraPicker.delegate = self 78 self.present(cameraPicker, animated: true, completion: nil) 79 } 80 else{ 81 print("error") 82 83 } 84 } 85 86 func tappedcamera() { 87 let sourceType:UIImagePickerController.SourceType = UIImagePickerController.SourceType.camera 88 // カメラが利用可能かチェック 89 if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){ 90 // インスタンスの作成 91 let cameraPicker = UIImagePickerController() 92 cameraPicker.sourceType = sourceType 93 cameraPicker.delegate = self 94 self.present(cameraPicker, animated: true, completion: nil) 95 96 } 97 else{ 98 print("error") 99 } 100 } 101 102 @IBAction func selecteImageButton(_ sender: UITapGestureRecognizer) { 103 104 //アラート表示のために 105 let actionSheet = UIAlertController(title: "", message: "写真の選択", preferredStyle: UIAlertController.Style.actionSheet) 106 107 let tappedcamera = UIAlertAction(title: "カメラで撮影", style: UIAlertAction.Style.default, handler: { 108 (action: UIAlertAction!) in 109 self.tappedcamera() 110 }) 111 112 let tappedlibrary = UIAlertAction(title: "ライブラリから選択", style: UIAlertAction.Style.default, handler: { 113 (action: UIAlertAction!) in 114 self.tappedlibrary() 115 }) 116 117 let cancel = UIAlertAction(title: "キャンセル", style: UIAlertAction.Style.cancel, handler: { 118 (action: UIAlertAction!) in 119 print("キャンセル") 120 }) 121 122 actionSheet.addAction(tappedcamera) 123 actionSheet.addAction(tappedlibrary) 124 actionSheet.addAction(cancel) 125 126 present(actionSheet, animated: true, completion: nil) 127 128 } 129 130}
もっと具体的に書けませんか?
少なくともソースコードとか貼ってほしいです。
自分が回答者側の立場になったときのことを考えてください。
回答2件
あなたの回答
tips
プレビュー