我有一个简单的标准图像选择器,除了在使用编辑器功能时图像会移到方形裁剪框上方之外,其他都可以正常工作。当我选择图像或调整图像大小然后接受图像时,裁切后的图像在裁切框上方约有20像素左右的图像,并且从底部砍下了约40像素。下面是代码。因为一张图片价值一千个单词,所以我提供了两个屏幕快照,以显示在编辑模式下的外观以及返回的内容。我将不胜感激任何明智的建议或明智的设置,这些建议可以使编辑人员准确地裁剪图片。
import UIKit
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)
}
}
答案 0 :(得分:2)
状态栏搞砸了。
添加以下扩展名可以使UIImagePickerControllerEditedImage
正确裁剪图像。
extension UIImagePickerController {
open override var childViewControllerForStatusBarHidden: UIViewController? {
return nil
}
open override var prefersStatusBarHidden: Bool {
return true
}
}
此扩展程序将状态栏隐藏在UIImagePickerController
中,看起来有些怪异,但结果很重要。
我从Swift 3.0 - how to hide status bar after calling UIImagePickerController?那里得到了
答案 1 :(得分:1)
感谢siefix!效果很好。
Swift 4.2
extension UIImagePickerController {
open override var childForStatusBarHidden: UIViewController? {
return nil
}
open override var prefersStatusBarHidden: Bool {
return true
}
}