How to allow user to pick the image with Swift?

后端 未结 21 886
[愿得一人]
[愿得一人] 2020-12-02 06:01

I am writing my first iOS application (iPhone only) with Swift. The main application view should allow user to choose the image from the photo gallery.

I\'ve found

21条回答
  •  广开言路
    2020-12-02 06:31

    You can do like here

    var avatarImageView = UIImageView()
    var imagePicker = UIImagePickerController()
            
    func takePhotoFromGallery() {
        imagePicker.delegate = self
        imagePicker.sourceType = .savedPhotosAlbum
        imagePicker.allowsEditing = true
        
        present(imagePicker, animated: true)
    }
    
    func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let pickedImage = info[.originalImage] as? UIImage {
            avatarImageView.contentMode = .scaleAspectFill
            avatarImageView.image = pickedImage
        }
        self.dismiss(animated: true)
    }
    

    Hope this was helpful

提交回复
热议问题