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.
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.
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.
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.
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.
You have to update the image rotation when you get UIImage
object.
Pass the image that is getting from image picker to this method
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!
}
+(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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With