xcode - Swift 2.0 Xcode 7.1 图像选择器前置摄像头图像在选择时旋转

标签 xcode swift ios9

我有一个显示带有 ImageView 和标签的人物的表格 View 。当一个人被插入到表格中时,它可以从联系人中拉出并且图像看起来很好。当一个人不是使用联系人添加时,它会从文本字段中提取姓名,并为相机显示一个图像选择器。如果我从后置摄像头拍照没有问题。如果我从前置摄像头拍照,则图片会根据屏幕方向关闭。即使在拍照时屏幕没有旋转,它也会根据以下情况关闭:

如果方向是纵向,则图像旋转 180 度。 如果方向是横向左,则图像向左旋转 90 度。 如果方向颠倒,则图像向右旋转 90 度。 如果方向是正确的横向,则图像很好。

这是我用来呈现图像选择器的代码:

func pickImage(){
    // create the alert controller, give it three actions: library, camera, cancel
    let pickerAlert = UIAlertController(title: "Add Pic?", message: "Would you like to add a picture for this person?", preferredStyle: .Alert)
    // create the library action
    let libAction = UIAlertAction(title: "Select photo from library", style: .Default) { (alert) -> Void in
        // set picker type to the library and present the picker
        self.picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        self.presentViewController(self.picker, animated: true, completion: nil)
    }
    // if the camera is available on the device create the camera action and add it to the picker alert
    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
        let camAction = UIAlertAction(title: "Take a picture", style: .Default) { (alert) -> Void in
            // set picker type to the camera and present the picker
            self.picker.sourceType = UIImagePickerControllerSourceType.Camera
            self.picker.modalPresentationStyle = .FullScreen
            self.presentViewController(self.picker, animated: true, completion: nil)
        }
        pickerAlert.addAction(camAction)
    }
    // create the cancel action, set the person's image to contacts
    let cancel = UIAlertAction(title: "Don't Add Image", style: .Cancel, handler: nil)

    pickerAlert.addAction(libAction)
    pickerAlert.addAction(cancel)

    self.presentViewController(pickerAlert, animated: true, completion: nil)
}

这是我的 imagePicker 委托(delegate):(不包括 didCancel,它只是取消选择器)

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
    let imgData = UIImagePNGRepresentation(image)
    saveToList(personName, imgData: imgData!)
    self.tableView.reloadData()
    self.dismissViewControllerAnimated(true, completion: nil)}

总不能是我把图片转成PNG数据了吧?这些方法也很基本。我对选择器没有太多了解。保存到列表方法只是获取名称和图像数据并将其存储在我的数据模型中。当我在单元格中检索图像时,我使用人的图像数据设置 imageview 图像。我已经阅读了图像选择器的 Apple 文档,但似乎无法找到我遗漏的内容:-(

最佳答案

换掉 UIImagePNGRepresentation 并改用 UIImageJPEGRepresentation。 PNG 不会自动保存方向信息。除非你正在做一个照片特定的应用程序,其中照片质量必须是完美的,否则 JPG 就可以了。

UIImageJPEGRepresentation 将请求图像数据以及 CGFloat 来设置压缩质量。从 0.0 到 1.0(最佳)。

祝你好运!

关于xcode - Swift 2.0 Xcode 7.1 图像选择器前置摄像头图像在选择时旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34794541/

相关文章:

swift - 在 Xcode 中使用 Parse 和 Swift 连接 PFUITableView 和详细信息 View

c++ - memset 导致 std::string 赋值崩溃

swift - 通过 Swift 在 Xcode 中保存和使用用户输入图像

swift - 为什么我在 AppDelegate 中导入 MessageUI 在 UITableViewController 中不可见?

ios - EXC_BAD_ACCESS KERN_INVALID_ADDRESS ios < 9.3 xcode 10

ios - 在./symbolicatecrash行115处无法在macosx SDK或任何后备SDK中找到名为 'otool'的工具

ios - 即使有,Xcode 也找不到 Bolts 框架

swift - 当数量超出 Swift 语言允许的最大数量时管理应用程序崩溃

swift - 如何声明表情符号字符变量并在 Swift 中打印它?

ios - NSStringboundingRectWithSize 在 iOS 9 上有 bug?