ios - 允许编辑时 UIImagePickerController 显示错误裁剪

标签 ios objective-c uiimage uiimagepickercontroller

我遇到了 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).在第二个屏幕截图中,如果您尝试将裁剪放到照片的最底部,它会弹回到这个位置,因此用户认为图像的底部不包括在内,而实际上是,当它被选中时.有点头疼。似乎在模拟器中和在设备上做同样的事情。有人知道为什么会这样吗?

Top crop bottom crop

最佳答案

这不能解决显示裁剪框 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/

相关文章:

ios - 单选按钮,如单选按钮

ios - 使用 swift 存储在应用程序中拍摄的 UIImages 的正确方法是什么?

ios - 无法对齐 TableView 单元格内容

ios - 如何从 Gmail 应用程序在 Safari 中打开链接?

ios - 如何将 NSstring 转换为 NSerror 对象类型

objective-c - 投影 GIF 文件大小的算法或数学?

iphone - 直接从应用程序转到地址簿/联系电话

ios - NSURLConnection 的分块传输编码有哪些替代方案

ios - 使用react-native在iOS中设置半透明statusBar

uiimageview - 从 'UIImageView*' 分配给 'UIImage*' 的不兼容指针类型