UIImagePickerController 未正确裁剪所选图像
UIImagePickerController not properly cropping selected image
我有一个简单的标准图像选择器,它工作正常,但当我使用编辑器功能时,图像会移动到方形裁剪框上方。当我 select 图像或调整图像大小然后接受它时,裁剪后的图像在裁剪框上方有大约 20 个像素的图像,底部有大约 40 个像素。下面是代码。因为一张图片胜过一千个字,所以我包含了两个屏幕截图来显示它在编辑模式下的样子以及我得到的结果。我将不胜感激任何允许编辑器准确裁剪图片的代码明智或设置明智的建议。
import UIKit
class ImagePickerVC: UIViewController {
@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var btnPickImage: UIButton!
@IBOutlet weak var btnCancel: UIBarButtonItem!
@IBOutlet weak var btnSave: UIBarButtonItem!
var imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
setupNavBarAppearance(control: self)
imagePicker.delegate = self
myImage.image = myUserProfile.avatar
}
@IBAction func btnCancel(_ sender: Any) {
dismiss(animated: false, completion: nil)
}
@IBAction func btnPickImage(_ sender: Any) {
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = true
present(imagePicker, animated: false, completion: nil)
}
@IBAction func btnSave(_ sender: Any) {
myUserProfile.avatar = myImage.image!
dismiss(animated: false, completion: nil)
}
}
extension ImagePickerVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
myImage.image = image
}
dismiss(animated: false, completion: nil)
}
}
状态栏搞砸了。
添加以下扩展可以使 UIImagePickerControllerEditedImage
正确裁剪图像。
extension UIImagePickerController {
open override var childViewControllerForStatusBarHidden: UIViewController? {
return nil
}
open override var prefersStatusBarHidden: Bool {
return true
}
}
这个扩展隐藏了 UIImagePickerController
中的状态栏,看起来有点奇怪,但结果很重要。
我从
那里得到的
感谢 siefix!这非常有效。
Swift 4.2
extension UIImagePickerController {
open override var childForStatusBarHidden: UIViewController? {
return nil
}
open override var prefersStatusBarHidden: Bool {
return true
}
}
我有一个简单的标准图像选择器,它工作正常,但当我使用编辑器功能时,图像会移动到方形裁剪框上方。当我 select 图像或调整图像大小然后接受它时,裁剪后的图像在裁剪框上方有大约 20 个像素的图像,底部有大约 40 个像素。下面是代码。因为一张图片胜过一千个字,所以我包含了两个屏幕截图来显示它在编辑模式下的样子以及我得到的结果。我将不胜感激任何允许编辑器准确裁剪图片的代码明智或设置明智的建议。
import UIKit
class ImagePickerVC: UIViewController {
@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var btnPickImage: UIButton!
@IBOutlet weak var btnCancel: UIBarButtonItem!
@IBOutlet weak var btnSave: UIBarButtonItem!
var imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
setupNavBarAppearance(control: self)
imagePicker.delegate = self
myImage.image = myUserProfile.avatar
}
@IBAction func btnCancel(_ sender: Any) {
dismiss(animated: false, completion: nil)
}
@IBAction func btnPickImage(_ sender: Any) {
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = true
present(imagePicker, animated: false, completion: nil)
}
@IBAction func btnSave(_ sender: Any) {
myUserProfile.avatar = myImage.image!
dismiss(animated: false, completion: nil)
}
}
extension ImagePickerVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
myImage.image = image
}
dismiss(animated: false, completion: nil)
}
}
状态栏搞砸了。
添加以下扩展可以使 UIImagePickerControllerEditedImage
正确裁剪图像。
extension UIImagePickerController {
open override var childViewControllerForStatusBarHidden: UIViewController? {
return nil
}
open override var prefersStatusBarHidden: Bool {
return true
}
}
这个扩展隐藏了 UIImagePickerController
中的状态栏,看起来有点奇怪,但结果很重要。
我从
感谢 siefix!这非常有效。
Swift 4.2
extension UIImagePickerController {
open override var childForStatusBarHidden: UIViewController? {
return nil
}
open override var prefersStatusBarHidden: Bool {
return true
}
}