3

我正在使用 UIImagePickerController 使用相机捕获图像。我的应用支持的方向是纵向。我看到 iPhone 5 的奇怪行为。我正在使用 Xcode 7 和 swift 2.0。iPhone 5 操作系统版本为 8.4。我的应用程序的部署目标是 8.0。

问题是。1. 对于 iPhone 5,拍摄图像后,图像会以拍摄图像的相应模式显示。但是在我按下“使用照片”标准选项后,当图像显示在 UIImageView 上时,图像会自动向左旋转。不知道为什么。如果我从照片库中选择图像,图像不会旋转。我不想旋转图像。我看到了类似的帖子,有更好的解释和实际图像,但没有得到回答。 UIImageView 使用视网膜 4 iPhone 模拟器而不是视网膜 3.5/常规模拟器旋转图像 我尝试了这篇文章中几乎所有的快速解决方案:iOS UIImagePickerController 上传后的结果图像方向和其他帖子,但似乎没有任何工作。我使用了 shouldAutorotate()、sFunc_imageFixOrientation(),并从这篇文章中添加了扩展。

  1. 此外,对于这两种设备,在按下“使用照片”选项后,上传图像大约需要 10 秒。能不能快点完成。

这是我的代码:

功能打开相机(){

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
        dispatch_async(dispatch_get_main_queue(), {
            let imagePicker = UIImagePickerController();
            imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
            imagePicker.allowsEditing = false;
            imagePicker.delegate = self;
            imagePicker.modalPresentationStyle = .FormSheet
            self.presentViewController(imagePicker, animated: true, completion: nil);
        });

    }
}

func openGallary() {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
        imagePicker.allowsEditing = true
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    profileImage?.image =   image
    self.dismissViewControllerAnimated(true, completion: nil);
}
4

1 回答 1

2

这是我观察到的。当您使用相机拍摄图像时,图像会逆时针旋转 90 度,并且图像方向设置为 UIImageOrientation.Up,因此来自函数 sFunc_imageFixOrientation iOS UIImagePickerController 的代码在上传 后将导致图像方向不起作用。如果您从照片库中选择图像,则图像将按原样显示。

修改功能:

    func sFunc_imageFixOrientation(img:UIImage) -> UIImage {

        // We need to calculate the proper transformation to make the image upright.
        // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
        var transform:CGAffineTransform = CGAffineTransformIdentity
        // Rotate image clockwise by 90 degree
        if (img.imageOrientation == UIImageOrientation.Up) {
            transform = CGAffineTransformTranslate(transform, 0, img.size.height);
            transform = CGAffineTransformRotate(transform,  CGFloat(-M_PI_2));
        }

        if (img.imageOrientation == UIImageOrientation.Down
            || img.imageOrientation == UIImageOrientation.DownMirrored) {

            transform = CGAffineTransformTranslate(transform, img.size.width, img.size.height)
            transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
        }

        if (img.imageOrientation == UIImageOrientation.Left
            || img.imageOrientation == UIImageOrientation.LeftMirrored) {

            transform = CGAffineTransformTranslate(transform, img.size.width, 0)
            transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
        }

        if (img.imageOrientation == UIImageOrientation.Right
            || img.imageOrientation == UIImageOrientation.RightMirrored) {

            transform = CGAffineTransformTranslate(transform, 0, img.size.height);
            transform = CGAffineTransformRotate(transform,  CGFloat(-M_PI_2));
        }

        if (img.imageOrientation == UIImageOrientation.UpMirrored
            || img.imageOrientation == UIImageOrientation.DownMirrored) {

            transform = CGAffineTransformTranslate(transform, img.size.width, 0)
            transform = CGAffineTransformScale(transform, -1, 1)
        }

        if (img.imageOrientation == UIImageOrientation.LeftMirrored
            || img.imageOrientation == UIImageOrientation.RightMirrored) {

            transform = CGAffineTransformTranslate(transform, img.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
        }


        // Now we draw the underlying CGImage into a new context, applying the transform
        // calculated above.
        let ctx:CGContextRef = CGBitmapContextCreate(nil, Int(img.size.width), Int(img.size.height),
                                                     CGImageGetBitsPerComponent(img.CGImage), 0,
                                                     CGImageGetColorSpace(img.CGImage),
                                                     CGImageGetBitmapInfo(img.CGImage).rawValue)!;
        CGContextConcatCTM(ctx, transform)


        if (img.imageOrientation == UIImageOrientation.Left
            || img.imageOrientation == UIImageOrientation.LeftMirrored
            || img.imageOrientation == UIImageOrientation.Right
            || img.imageOrientation == UIImageOrientation.RightMirrored
            || img.imageOrientation == UIImageOrientation.Up
            ) {

            CGContextDrawImage(ctx, CGRectMake(0,0,img.size.height,img.size.width), img.CGImage)
        } else {
            CGContextDrawImage(ctx, CGRectMake(0,0,img.size.width,img.size.height), img.CGImage)
        }


        // And now we just create a new UIImage from the drawing context
        let cgimg:CGImageRef = CGBitmapContextCreateImage(ctx)!
        let imgEnd:UIImage = UIImage(CGImage: cgimg)

        return imgEnd
    }
}

仅当从相机捕获图像时才调用此函数。我知道我的答案并不完美,但如果没有什么适合您的情况,您绝对可以尝试。

于 2016-07-01T01:54:09.750 回答