After getting image from UIImagePickerController,

2020-04-10 22:50发布

I am using UIImagePickerController to capture image using camera. Supported orientation for my app is Portrait. I am seeing strange behavior for iPhone 5. I am using Xcode 7 and swift 2.0. iPhone 5 OS version is 8.4. Deployment Target is 8.0 for my app.

Issues are. 1. For iPhone 5, after capturing image, image is shown in respective mode in which image is captured. But After I press 'Use Photo' standard option and when image is displayed on UIImageView, image is automatically rotated to left. Don't know why. If I choose image from photo library, image is not rotated. I don't want image to be rotated. I seen similar post with better explanation and actual image but is not answered. UIImageView rotates image with retina 4 iPhone simulator but not retina 3.5/regular simulator I tried almost all swift solution from this post : iOS UIImagePickerController result image orientation after upload and other post too but nothing seems to be working. I used shouldAutorotate(), sFunc_imageFixOrientation(), and adding extension from this post.

  1. Also, for both device, after pressing 'Use Photo' option, it takes around 10 second to upload image. Can it be done faster.

Here is my code:

func openCamera() {

    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);
}

1条回答
叼着烟拽天下
2楼-- · 2020-04-10 23:20

This is what I observed. When you capture image using camera, image is rotated counterclockwise by 90 degree and image orientation is set to UIImageOrientation.Up so code from function sFunc_imageFixOrientation iOS UIImagePickerController result image orientation after upload will not work. If you select image from photo library, then image will be displayed as it is.

Modified function:

    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
    }
}

Call this function only if image is captured from Camera. I know my answer will not be perfect but you can definitely try if nothing is working out for you.

查看更多
登录 后发表回答