我遇到了 UIImagePickerController
的问题,我启用了编辑功能,但发生的情况是当用户裁剪图像时,正方形裁剪显示用户将看到的图像之间存在 y 偏移量裁剪,以及实际裁剪是什么。它只适用于大约 20 像素,但现在是一个大问题。我已经包含了两个屏幕截图来演示该问题。 In the first, the crop square seems to extend above the image, but when the inage is chosen the top of the image is set properly (the image is just a screenshot so the top of the image is the top of the status bar).在第二个屏幕截图中,如果您尝试将裁剪放到照片的最底部,它会弹回到这个位置,因此用户认为图像的底部不包括在内,而实际上是,当它被选中时.有点头疼。似乎在模拟器中和在设备上做同样的事情。有人知道为什么会这样吗?
最佳答案
这不能解决显示裁剪框 20 像素偏移的显示问题,但您可以通过在用户选择图像时执行以下操作来避免最终裁剪图像错误:
// MARK: - UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: { [weak self] in self?.didCompleteSelection(image: nil) })
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion: { [weak self] in
guard
let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage,
let cropRect = info[UIImagePickerControllerCropRect] as? CGRect
else { return }
//crop with status-bar offset due to iOS bug introduced in iOS 8 with status bar offset
let offsetRect = cropRect.offsetBy(dx: 0, dy: UIApplication.shared.statusBarFrame.size.height)
guard let croppedImage = originalImage.cgImage?.cropping(to: offsetRect) else { return }
self?.didCompleteSelection?(image: croppedImage)
})
}
关于ios - 允许编辑时 UIImagePickerController 显示错误裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28218820/