【问题标题】:How to make UIImagePickerController for photo library and my problem如何为照片库制作 UIImagePickerController 和我的问题
【发布时间】:2020-03-20 09:46:26
【问题描述】:

我使用 UIImagePickerController 并且我希望我的应用程序允许我从照片库中选择一张照片。
我有一个界面屏幕:

我有图像视图,我希望在单击图像视图时打开画廊。接下来,我选择了一张图片,并将其插入到这个圈子中。

我在照片库中选择了一张图片后,我的图片没有显示在圆圈中。请帮帮我:(

我的代码:

import UIKit

class ProfileSettingsViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate {

  @IBOutlet weak var cancelButton: UIButton!
  @IBOutlet weak var uploadNewPhoto: UIImageView!


  override func viewDidLoad() {
    super.viewDidLoad()
    //    installCancelButtonColor()
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ProfileSettingsViewController.imageTapped(gesture:)))
    uploadNewPhoto.addGestureRecognizer(tapGesture)
    uploadNewPhoto.isUserInteractionEnabled = true
  }

  @objc func imageTapped(gesture: UIGestureRecognizer) {
    // if the tapped view is a UIImageView then set it to imageview
    if (gesture.view as? UIImageView) != nil {
      print("Image Tapped")
      let imageC = UIImagePickerController()
      imageC.delegate = self
      imageC.sourceType = UIImagePickerController.SourceType.photoLibrary
      present(imageC, animated: true)

    }
  }

  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
      picker.dismiss(animated: true)

      guard let image = info[.editedImage] as? UIImage else {
          print("No image found")
          return
      }

      // print out the image size as a test
      print(image.size)
  }

  //  func installCancelButtonColor() {
  //    cancelButton.layer.borderWidth = 1.5
  //    let cancelButtonColor = UIColor(red: 0.20, green: 0.28, blue: 0.36, alpha: 1)
  //    cancelButton.layer.borderColor = cancelButtonColor.cgColor
  //  }


  @IBAction func cancelButton(_ sender: Any) {
    dismiss(animated: true, completion: nil)
  }
}

【问题讨论】:

  • 什么不起作用?明确说明您的错误
  • @Toto 我在照片库中选择了一张图片后,我的图片没有显示在圆圈中。

标签: swift uikit uiimagepickercontroller


【解决方案1】:

要在 UIImageView 中查看图像,您需要进行设置。

 if let image = info[.editedImage] as? UIImage {
     uploadNewPhoto.image = image
 }

如果您没有从didFinishPickingMediaWithInfo 获取图像,请尝试将.editedImage 替换为.originalImage

并且最好在您打开PhotoLibrary这个时指定

imageC.mediaTypes = ["public.image"] // this will allow the user to see and select photos only

【讨论】:

    猜你喜欢
    • 2020-09-07
    • 2017-06-02
    • 2014-09-20
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多