質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2024閲覧

カメラロールから取得した画像をUIImageViewに表示したい

mimamo

総合スコア44

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/01/25 13:42

前提・実現したいこと

カメラロールで写真を選択すると、その写真のアスペクト比にあったUIImageViewを作成・写真表示できるようにしたいです。
初歩的なことですが解決できずに悩んでいます、、
ご教授よろしくお願いいたします。

発生している問題・エラーメッセージ

関数 imagePickerController のgetphotoの部分で

Use of unresolved identifier 'getphoto'

該当のソースコード

swift

1 2class ViewController2: UIViewController, UIGestureRecognizerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate { 3 4 static let safeAreaBottomInset: CGFloat = { 5 if #available(iOS 11.0, *) { 6 return UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets.bottom ?? 0 7 } else { 8 return 0 9 } 10 }() 11 12 var screenWidth:CGFloat = 0 13 var screenHeight:CGFloat = 0 14 15 // ステータスバーの高さを取得する 16 let statusBarHeight = UIApplication.shared.statusBarFrame.size.height 17 18 19 override func viewDidLoad() { 20 super.viewDidLoad() 21 //UIImageViewのインスタンス作成 22 let getphoto = UIImageView() 23 self.view.addSubview(getphoto) 24 25 // Screen Size の取得 26 screenWidth = self.view.bounds.width 27 screenHeight = self.view.bounds.height 28 } 29 30 31 //カメラロールから写真を選択する処理 32 @IBAction func choosePicture() { 33 // カメラロールが利用可能か? 34 if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { 35 // 写真を選ぶビュー 36 let pickerView = UIImagePickerController() 37 // 写真の選択元をカメラロールにする 38 // 「.camera」にすればカメラを起動できる 39 pickerView.sourceType = .photoLibrary 40 // デリゲート 41 pickerView.delegate = self 42 // ビューに表示 43 self.present(pickerView, animated: true) 44 } 45 } 46 47 48 // 写真を選んだ後に呼ばれる処理 49 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 50 // 選択した写真を取得する 51 let image = info[UIImagePickerControllerOriginalImage] as! UIImage 52 53 54 getphoto.frame = CGRect(x: 0, y: 0, width: screenWidth / 3, height: screenHeight / 3).aspectFit(contentSize: image.size, stretchble: true, integer: true) 55 56 57 getphoto.center = CGPoint(x:screenWidth/2, y:(screenHeight-statusBarHeight-88-ViewController2.safeAreaBottomInset)/2+statusBarHeight+44) 58 59 60 // ビューに表示する 61 self.getphoto.image = image 62 63 // 写真を選ぶビューを引っ込める 64 self.dismiss(animated: true) 65 } 66} 67 68extension CGRect { 69 func aspectFit(contentSize: CGSize, stretchble: Bool, integer: Bool) -> CGRect { 70 let xZoom = width / contentSize.width 71 let yZoom = height / contentSize.height 72 let zoom = stretchble ? min(xZoom, yZoom) : min(xZoom, yZoom, 1) 73 74 if integer { 75 let newWidth = max(Int(contentSize.width * zoom), 1) 76 let newHeight = max(Int(contentSize.height * zoom), 1) 77 let newX = Int(origin.x) + (Int(width) - newWidth) / 2 78 let newY = Int(origin.y) + (Int(height) - newHeight) / 2 79 return CGRect(x: newX, y: newY, width: newWidth, height: newHeight) 80 } else { 81 let newWidth = contentSize.width * zoom 82 let newHeight = contentSize.height * zoom 83 let newX = origin.x + (width - newWidth) / 2 84 let newY = origin.y + (height - newHeight) / 2 85 return CGRect(x: newX, y: newY, width: newWidth, height: newHeight) 86 } 87 } 88} 89 90

補足情報(FW/ツールのバージョンなど)

Swiftのversionは4.1.2
Xcodeのversionは9.4.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

全体がわからないので、解決になるかわかりませんが、
override func viewDidLoad() {のなかで
getphotoがローカル変数として定義されているのに

imagePickerControllerで、あたかもグローバル変数のように
self.getphoto と用いいているので、エラーが出ていると思います。

先ずはそこを直してみてください。

投稿2019/01/27 14:12

hameji001

総合スコア639

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mimamo

2019/01/28 01:09

ご回答ありがとうございます! getphotoについて関数を作ることで解決しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問