【问题标题】:Can't pan image taken from camera with UIImagePicker无法使用 UIImagePicker 平移从相机拍摄的图像
【发布时间】:2017-06-02 01:43:28
【问题描述】:

我正在使用 UIImagePicker 让用户拍照。拍摄照片时,我希望他们平移和缩放图像以适应裁剪框,以便图像存储为正方形。

但是,在裁剪图像时,您似乎无法将其移动到(纵向)图像的顶部和底部(如果是横向则为左侧和右侧)。

我尝试过搜索,但似乎没有太多信息,但这似乎是一个大问题。

有人可以帮忙吗?

这是我正在使用的极少量代码:

let imagePicker = UIImagePickerController()

imagePicker.allowsEditing = true
imagePicker.sourceType = UIImagePickerControllerSourceType.camera

present(imagePicker, animated: true, completion: nil)

显然还有更多代码,但这是主要部分。

用照片编辑:

所以我希望能够移动/放大照片以选择要保存的任何正方形部分。但是,我无法将它从这个位置移动/一直弹回来。

我可以放大,但它仍然限制我从顶部和底部边缘。

同样,它适用于照片库。

【问题讨论】:

  • 让 myPickerController = UIImagePickerController() myPickerController.delegate = self; myPickerController.allowsEditing = true myPickerController.sourceType = UIImagePickerControllerSourceType.Camera self.presentViewController(myPickerController, animated: true, completion: nil)
  • 你是在告诉我将委托设置为自我吗?因为我已经这样做了
  • 您检查的是哪个设备? iphone 6 还是别的?
  • 它只需要适用于iPhone 5S
  • 这是自 iOS 6 以来的一个错误,看起来他们出于某种原因不会修复它。我建议使用替代方法,例如 github.com/justwudi/WDImagePicker。另请查看 2012 年的这篇帖子,其中包含相同的错误:stackoverflow.com/questions/12630155/…

标签: ios swift xcode uiimage uiimagepickercontroller


【解决方案1】:

我知道你说它已经缩放了,但你可能只需要为此调整边界。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    image.image = info[UIImagePickerControllerEditedImage] as? UIImage
    self.dismiss(animated: true, completion: nil)
}

您可能还需要使用 CGRect 在屏幕中正确设置图像。它应该居中。如果您使用的是 iphone 5,则尺寸为 640 x 1136。

发生这种情况是因为图像的宽度或高度超出了屏幕范围。

【讨论】:

    【解决方案2】:

    我使用了以下链接提供的解决方案:-

    https://github.com/Hipo/HIPImageCropper

    它处理图像的横向和纵向对齐,并通过裁剪功能提供放大和缩小。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      这是 iOS 6 中引入的错误,尚未修复。

      2012 年为此提出了雷达,但被 Apple 关闭。我设法再次打开它,并且在过去 6 个月里一直在我的联系人中纠缠 Apple 开发人员。

      http://openradar.appspot.com/12318774

      在 Apple 解决此问题之前,唯一的选择是使用第三方控件或自己动手。

      这是我打开的雷达……

      http://openradar.appspot.com/28260087

      【讨论】:

      • 哇。原来的雷达是2012年开的,到2021年这个bug依然存在。“归档雷达!”开什么玩笑
      【解决方案4】:

      如果您在 info.plist 中将“查看基于控制器的状态栏外观”设置为 NO,并使用以下命令将状态栏外观设置为浅色

      UIApplication.shared.statusBarStyle = .lightContent
      

      或使用任何其他方法,然后只需在呈现图像选择器之前将样式设置为 .default。例如:

      imagePicker.allowsEditing = true
      imagePicker.sourceType = .photoLibrary
      UIApplication.shared.statusBarStyle = .default
      present(imagePicker, animated: true, completion: nil)
      

      根据您的需要将源类型更改为 photoLibrary 或相机,并在您的 didFinishPickingMediaWithInfo 的完成块中将以下内容添加到完成块中。

      func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
          //let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
      
          var pickedImage : UIImage?
          if let img = info[UIImagePickerControllerEditedImage] as? UIImage
          {
              pickedImage = img
      
          }
          else if let img = info[UIImagePickerControllerOriginalImage] as? UIImage
          {
              pickedImage = img
          }
          dismiss(animated: true, completion: {
              UIApplication.shared.statusBarStyle         = .lightContent
          })}
      

      显然这是一个解决方法。希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2010-12-21
        • 1970-01-01
        • 2011-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多