无法在UIImagePicker中拍摄的图像上进行平移

9
我正在使用UIImagePicker让用户拍照。当拍摄完成后,我希望他们可以平移和缩放图像以适应裁剪框内,从而将图像存储为正方形。
然而,在裁剪图像时,似乎无法将其移动到(肖像模式下的)图像的顶部和底部(在横向模式下则是左侧和右侧)。
我尝试过搜索,但似乎没有太多信息,但这似乎是一个巨大的问题。
有人能帮忙吗?
以下是我正在使用的非常少量代码:
let imagePicker = UIImagePickerController()

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

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

显然还有更多的代码,但这是主要部分。
附带照片的编辑:

enter image description here

所以我希望能够移动照片/缩放以选择任何正方形部分进行保存。但是,我无法从此位置移动它/它会立即弹回。
我可以缩放,但仍然不能超出顶部和底部边缘。
同样,在照片库中可以正常工作。

let myPickerController = UIImagePickerController() myPickerController.delegate = self; myPickerController.allowsEditing = true myPickerController.sourceType = UIImagePickerControllerSourceType.Camera self.presentViewController(myPickerController, animated: true, completion: nil) - Himanshu Moradiya
你是在告诉我将委托设置为self吗?因为我已经这样做了。 - user2397282
它只需要在iPhone 5S上工作 - user2397282
在 iPhone 5s 上,我从未遇到过这种问题。一个建议是将您的应用程序从设备中删除,然后清理项目并再次运行它,再次检查此问题。 - Himanshu Moradiya
1
这个问题从iOS 6开始就存在了,看起来他们似乎没有理由去修复它。我建议使用替代方案,例如https://github.com/justwudi/WDImagePicker。同时,可以查看一下2012年的这篇帖子,里面描述了完全相同的bug:https://dev59.com/questions/jGcs5IYBdhLWcg3wtmSf。 - Imbue
显示剩余4条评论
4个回答

9

这是一个在iOS 6中引入并尚未修复的错误。

2012年曾经有一份报告提出了这个问题,但被苹果关闭了。我设法重新开启了这个报告,并在过去的六个月里一直向我的苹果开发人员联系人催促他们处理此问题。

http://openradar.appspot.com/12318774

在苹果修复这个问题之前,唯一的选择是使用第三方控件或自己解决。

以下是我所提出的报告链接...

http://openradar.appspot.com/28260087


3
哇,最初的雷达于2012年启用,到2021年今天这个漏洞仍然存在。“提交一个雷达!”简直是个笑话。 - TylerJames
仍然无法在 iPhone 13,iOS 15.4 上运行。 - Soorej Babu

0

我已经使用了下面链接提供的解决方案:

https://github.com/Hipo/HIPImageCropper

它处理图像的横向和纵向对齐,并提供缩放和裁剪功能。
希望能对您有所帮助。

0
如果您在info.plist中将“View controller-based status bar appearance”设置为NO,并使用以下代码设置状态栏外观为浅色:
UIApplication.shared.statusBarStyle = .lightContent

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

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

根据您的需求更改源类型,可以是photoLibrary或camera,并在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
    })}

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


这给了我们希望,但似乎并没有起作用。如果有人通过使用这个解决方法解决了问题,请让其他程序员知道。 - Raimundas Sakalauskas

0

我知道你已经说过它可以缩放,但你可能需要调整一下边界。

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。

这发生的原因是图像的宽度或高度被最大化到屏幕上。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接