swift 2只在方形模式下打开相机

swift 2 open camera in square mode only

到目前为止我的相机工作正常,它以常规格式打开。但我希望唯一的选择是方形格式。

到目前为止,这是我的代码。

@IBAction func takePhoto(sender: AnyObject) {
    let picker = UIImagePickerController()

    picker.delegate = self
    picker.sourceType = .Camera

    presentViewController(picker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    dismissViewControllerAnimated(true, completion: nil)
}

我认为您无法使用 UIImagePickerController 以方形格式打开相机。您可以做的是将 UIImagePickerController allowEditing 设置为 YES,然后从结果字典中使用键 UIImagePickerControllerEditedImage 然后您将拥有方形图像,因此您的代码变为 .

@IBAction func takePhoto(sender: AnyObject) {
    let picker = UIImagePickerController()

    picker.delegate = self
    picker.sourceType = .Camera
    picker.allowsEditing = true

    presentViewController(picker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imageView.image = info[UIImagePickerControllerEditedImage] as? UIImage
    dismissViewControllerAnimated(true, completion: nil)
}

answer 中提到了另一种解决方法。 您可以使用答案中的逻辑在 swift.

中编写代码

但是如果您想查看详细信息并尝试做更多的事情,请查看

也看看苹果sample.