0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Swift】アルバム起動

Posted at

①photosをインポートする

import Photos

② UIImagePickerControllerDelegate,UINavigationControllerDelegate を継承する。

UIImagePickerControllerDelegate,UINavigationControllerDelegate

③以下のコードをメソッドとして記述する。

//アルバムを起動
    func doAlbum(){
        let sourceType:UIImagePickerController.SourceType = .photoLibrary
        //カメラが利用可能かチェックする
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
            let cameraPicker = UIImagePickerController()
            cameraPicker.sourceType = sourceType
            cameraPicker.delegate = self
            cameraPicker.allowsEditing = true
            present(cameraPicker, animated: true, completion: nil)
        }
    }

④アルバムから画像が選択されたときの呼ばれる箇所。以下のコードを記述する。

//カメラ撮影orアルバムから画像選択された時に呼ばれる
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
    if info[.originalImage] as? UIImage != nil{
        let selectedImage = info[.originalImage] as! UIImage
        UserDefaults.standard.set(selectedImage.jpegData(compressionQuality: 0.1), forKey: "userImage")
        logoImageView.image = selectedImage
        picker.dismiss(animated: true, completion: nil)
        
    }
}

※理解のため、自分のわかりやすい単語に置き換えてあるので、表記に問題ありの場合が多いかと思いますが、ご了承ください。。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?