iOS Swift Detecting Image Rotation

iOS Swift Detecting Image Rotation

It is quite simple, this is the full solution:

Sample Setup:

imageView.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 6) // just to test (it is 30 in degrees and 0.523598775598299 in radians)

Code:

let rad: Double = atan2( Double(imageView.transform.b), Double(imageView.transform.a))
let deg: CGFloat = CGFloat(rad) * (CGFloat(180) / CGFloat.pi )
print(deg) // works, printing 30

where deg = degrees and rad = radians

Explanation:
The first line is getting the radians, and the second line is multiplying the radians by the equivalent of a radian in degrees, to get the degrees.


NOTES:

  1. In CGAffineTransform(rotationAngle: someValue), someValue is, in fact, the radians of the angle, it is not measured in degrees. More information about:

    • radian

    • degree

    • PI

  2. The value in degrees of the radian CGFloat.pi is 180, therefore you can test it for any angle depending on this.


Let me know if this helps!

Swift - How to detect orientation changes

let const = "Background" //image name
let const2 = "GreyBackground" // image name
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()

imageView.image = UIImage(named: const)
// Do any additional setup after loading the view.
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if UIDevice.current.orientation.isLandscape {
print("Landscape")
imageView.image = UIImage(named: const2)
} else {
print("Portrait")
imageView.image = UIImage(named: const)
}
}

Ensuring Image Data is correctly oriented on iOS App in Swift 5

Thanks for reaching out to us, I'm Julie from MLKit team, sorry to catch up this thread late.

Yes, when a photo is captured from the camera, the default orientation is not always .up, e.g., if it is taken in a portrait mode, the orientation of the image.orientation is .right.

Face detector is actually quite flexible in handling images whose orientation is not .up, the key step is to set the orientation correctly:

Here is an example of detecting faces using photos captured from camera in our quickstart app, check it out to see if it will solve your problem.

Basically you only need to set the imageMetadata.orientation value correctly like this:

    // Define the metadata for the image.
let imageMetadata = VisionImageMetadata()
imageMetadata.orientation = UIUtilities.visionImageOrientation(from: image.imageOrientation)

// Initialize a VisionImage object with the given UIImage.
let visionImage = VisionImage(image: image)
visionImage.metadata = imageMetadata

and the mapping between the orientations can be found here:

public static func visionImageOrientation(
from imageOrientation: UIImage.Orientation
) -> VisionDetectorImageOrientation {
switch imageOrientation {
case .up:
return .topLeft
case .down:
return .bottomRight
case .left:
return .leftBottom
case .right:
return .rightTop
case .upMirrored:
return .topRight
case .downMirrored:
return .bottomLeft
case .leftMirrored:
return .leftTop
case .rightMirrored:
return .rightBottom
}
}

This statement for UIImage is for a more general purpose across all ML Kit detectors:


Create a VisionImage object using the correctly-rotated UIImage. Do not specify any rotation metadata—the default value, .topLeft, must be used.

but for face, it can be handled in a lightweight way by just setting the orientation correctly. We apologize for the confusion that it brought to you, and we will update this statement in the next release.

Thanks for reporting the problem, and hope the quickstart app would be helpful to your development.

Cheers,

Julie

iPhone, how to detect orientation of image when it was taken

You can use myImage.imageOrientation, which will give you the UIImageOrientation,

Or if for some reason you want the EXIF information directly in the "imagePickerController:didFinishPickingMediaWithInfo:" method.

by accessing the info dictionary [info objectForKey:@"Orientation"];

Note: This will not relate to UIImageOrientation directly the correlation is as follows.


- (UIImageOrientation)orientationFromEXIF:(int)exif
{
UIImageOrientation newOrientation;
switch (exif)
{
case 1:
newOrientation = UIImageOrientationUp;
break;
case 3:
newOrientation = UIImageOrientationDown;
break;
case 8:
newOrientation = UIImageOrientationLeft;
break;
case 6:
newOrientation = UIImageOrientationRight;
break;
case 2:
newOrientation = UIImageOrientationUpMirrored;
break;
case 4:
newOrientation = UIImageOrientationDownMirrored;
break;
case 5:
newOrientation = UIImageOrientationLeftMirrored;
break;
case 7:
newOrientation = UIImageOrientationRightMirrored;
break;
}
return newOrientation;
}

But you're better off using .imageOrientation

Swift How can I bring my Image after moving it back to the Middle ? Or rotate it back to the Origin position

If you just want to undo a previous CGAffineTransform that you used to move or rotate your view you can use view.transform = CGAffineTransform.identity. (Also, delete the use of self from your code because self.view and the view inside your func are different things.)

Determine if UIImageView has been rotated

If you just want to tell if the image has been rotated using an affine transform, as your question implies, you can do this:

if (CGAffineTransformIsIdentity(photoView.transform)) { 
// not rotated
...
} else {
// rotated
...
}

If you want to check for a particular rotation, do this:

CGAffineTransform t = CGAffineTransformMakeRotation(M_PI);
if (CGAffineTransformEqualToTransform(photoView.transform, t)) {
// rotated by M_PI
...
}

Note that the above two solutions only work if you are not applying OTHER affine transforms to the view at the same time as the rotation transforms. If you are also applying other transforms, perhaps you are better off just tracking the rotation state in a variable.



Related Topics



Leave a reply



Submit