Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: How can a custom camera save images always in the right orientation?

Tags:

ios

iphone

I'm currently implementing a custom camera with IOS. I want the camera to save images to album always in the correct direction just like the official iOS camera: no matter in what orientation you hold the camera you can always see the photo save the correct orientation. I checked many source code, they seem to save the image rather arbitrarily with the following:

var image  = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)

But this only works when the device is upright Could anyone provide any suggestions?

I also tried to get the orientation

let uiorientation = UIApplication.shared.statusBarOrientation

But it always returns portrait when the screen is portrait locked.

like image 884
ZijunLost Avatar asked Oct 17 '22 13:10

ZijunLost


People also ask

How do you change iPhone camera orientation?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off. Turn your iPhone sideways.

Why does my iPhone keep rotating my pictures?

The feature that's causing your photos to rotate and remain oriented the same way at all times is called Auto-Rotate, and it's actually a very good feature because it ensures that you'll be able to read whatever text is on the screen regardless of how you choose to hold your phone.

How do I stop my iPhone camera from flipping photos?

To lock the screen in portrait orientation, swipe up from the bottom edge of the screen to open Control Center, then tap (lock with circle arrow icon). The Portrait orientation lock icon (lock with circle arrow icon) appears in the status bar when the screen orientation is locked.

What are preserve settings?

Preserve Settings The trick is to use the iPhone's Preserve Settings feature. To set it up, open the Settings app and navigate to Camera > Preserve Settings. You have several options to choose from: Camera Mode, Creative Controls, Exposure Adjustment, Night Mode, and Live Photo.


1 Answers

You have to update the image rotation when you get UIImage object. Pass the image that is getting from image picker to this method

Swift

func getNormalizedImage(rawImage: UIImage) -> UIImage {

    if (rawImage.imageOrientation == .up) {
        return rawImage
    }

    UIGraphicsBeginImageContextWithOptions(rawImage.size, false, rawImage.scale)
    rawImage.draw(in: CGRect(x:0, y:0, width:rawImage.size.width, height:rawImage.size.height))

    let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return normalizedImage!
}

Objective-C

+(UIImage *)getNormalizedImage:(UIImage *)rawImage {

    if(rawImage.imageOrientation == UIImageOrientationUp)
    return rawImage;

    UIGraphicsBeginImageContextWithOptions(rawImage.size, NO, rawImage.scale);
    [rawImage drawInRect:(CGRect){0, 0, rawImage.size}];
    UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return normalizedImage;
}
like image 70
Abhishek Thapliyal Avatar answered Nov 15 '22 12:11

Abhishek Thapliyal