Swift 2.0 Xcode 7.1图像选择器前置摄像头图像旋转后被拾取 [英] Swift 2.0 Xcode 7.1 image picker front camera image rotated when picked

查看:68
本文介绍了Swift 2.0 Xcode 7.1图像选择器前置摄像头图像旋转后被拾取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a tableview that displays people with an imageview and label. When a person is inserted into the table it can pull from contacts and the images look fine. When a person is not added using contacts it pulls the name from a text field and an image picker is presented for the camera. If I take a picture from the rear camera no issues. If I take a picture from the front facing camera, then the picture is off based on screen orientation. Even if the screen isn't rotated when taking the picture it is off based on the following:

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

If orientation is portrait, image is rotated 180 degrees. If orientation is landscape left, image is rotated 90 degrees left. if orientation is upside down, image is rotated 90 degrees right. if orientation is landscape right, image is fine.

这是我展示图片选择器的代码:

Here is my code to present the image picker:

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委托:(不包括didCancel,它只是关闭了选择器)

Here is my imagePicker delegate: (not including didCancel, it just dismisses the picker)

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数据吗?这些方法也是非常基本的.我没有太多的选择. 保存到列表"方法仅使用名称和图像数据,并将其存储在我的数据模型中.当我在单元格中检索图像时,我使用人员的图像数据设置了图像视图图像.我已经阅读了有关图像选择器的Apple文档,但似乎找不到我所缺少的东西:-(

Surely it can't be me converting the image to PNG data? Those methods are pretty basic too. I don't have a lot going on with the picker. The save to list method simply takes the name and image data and stores it in my data model. When I retrieve the image in the cell I set the imageview image using the person's image data. I have read through the Apple documentation for the image picker and can't seem to find what I am missing :-(

推荐答案

换出UIImagePNGRepresentation并改用UIImageJPEGRepresentation. PNG不会自动保存方向信息.除非您使用的照片专用应用程序必须具有完美的照片质量,否则JPG很好.

Swap out the UIImagePNGRepresentation and use UIImageJPEGRepresentation instead. PNGs aren't saved with orientation information automatically. Unless you are doing a photo specific app where photo quality has to be perfect, JPGs are fine.

UIImageJPEGRepresentation会询问图像数据,但也会询问CGFloat来设置压缩质量.从0.0到1.0(最佳).

The UIImageJPEGRepresentation will ask for the image data but also a CGFloat to set the compression quality. It's from 0.0 to 1.0 (best).

祝你好运!

这篇关于Swift 2.0 Xcode 7.1图像选择器前置摄像头图像旋转后被拾取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆