CameraManager icon indicating copy to clipboard operation
CameraManager copied to clipboard

Image rotates automatically

Open shivamgarg102 opened this issue 5 years ago • 8 comments

** After capturing image, when i place that image on same ViewController, sometimes it rotates automatically. Please suggest me something to resolve this issue. Thanks! ** 1567172727467

shivamgarg102 avatar Aug 30 '19 13:08 shivamgarg102

Same issue happening with me. If I capture image in landscape mode two times and if I capture image third time the output image should be portrait while it showing landscape.

ashwaniInsigma avatar Sep 23 '19 11:09 ashwaniInsigma

I was facing the same issue, I used the following extension to rotate the image based on device rotation:

` extension UIImage {

/// Fix image orientaton to protrait up
func fixedOrientation() -> UIImage? {
    guard imageOrientation != UIImage.Orientation.up else {
        // This is default orientation, don't need to do anything
        return self.copy() as? UIImage
    }
    
    guard let cgImage = self.cgImage else {
        // CGImage is not available
        return nil
    }
    
    guard let colorSpace = cgImage.colorSpace, let ctx = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
        return nil // Not able to create CGContext
    }
    
    var transform: CGAffineTransform = CGAffineTransform.identity
    
    switch imageOrientation {
    case .down, .downMirrored:
        transform = transform.translatedBy(x: size.width, y: size.height)
        transform = transform.rotated(by: CGFloat.pi)
    case .left, .leftMirrored:
        transform = transform.translatedBy(x: size.width, y: 0)
        transform = transform.rotated(by: CGFloat.pi / 2.0)
    case .right, .rightMirrored:
        transform = transform.translatedBy(x: 0, y: size.height)
        transform = transform.rotated(by: CGFloat.pi / -2.0)
    case .up, .upMirrored:
        break
    @unknown default:
        fatalError("Missing...")
        break
    }
    
    // Flip image one more time if needed to, this is to prevent flipped image
    switch imageOrientation {
    case .upMirrored, .downMirrored:
        transform = transform.translatedBy(x: size.width, y: 0)
        transform = transform.scaledBy(x: -1, y: 1)
    case .leftMirrored, .rightMirrored:
        transform = transform.translatedBy(x: size.height, y: 0)
        transform = transform.scaledBy(x: -1, y: 1)
    case .up, .down, .left, .right:
        break
    @unknown default:
        fatalError("Missing...")
        break
    }
    
    ctx.concatenate(transform)
    
    switch imageOrientation {
    case .left, .leftMirrored, .right, .rightMirrored:
        ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
    default:
        ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        break
    }
    
    guard let newCGImage = ctx.makeImage() else { return nil }
    return UIImage.init(cgImage: newCGImage, scale: 1, orientation: .up)
}

}`

rameezhandel avatar Oct 10 '19 10:10 rameezhandel

@shivamgarg102 @ashwaniInsigma @rameezhandel

I tested taking pictures horizontally and vertically, both with and without device rotation locked and couldn't reproduce it. Do you have more information about this issue?

torrao avatar Apr 06 '20 14:04 torrao

@torrao Run app in portrait orientation, rotate to landscape left, rotate back to portrait, take photo. Your image will be rotated left. Settings: shouldRespondToOrientationChanges = true shouldKeepViewAtOrientationChanges = true

ooleynich avatar Apr 17 '20 09:04 ooleynich

@ooleynich

to help me replicate the issue, can you please provide some info:

  • are you running the Example App?
  • which version of CameraManager are you using;
  • which device and iOS version are you running on;

Thanks

torrao avatar Apr 17 '20 14:04 torrao

Hmm... i am having the same issue. All I did was creating a app with viewcontroller and did folloring

private let cameraMan: CameraManager = CameraManager()

func setup(view: UIView) {
    cameraMan.shouldRespondToOrientationChanges = false
    cameraMan.shouldKeepViewAtOrientationChanges = false
    cameraMan.addPreviewLayerToView(view)
}

And I called setup in viewDidLoad() and passed view from viewcontroller into setup function.

y25zhao avatar Dec 28 '20 09:12 y25zhao

On top of this, this issue also happens with example app from this repo. Half of the screen is cut too. I am using my iPad Pro 9 inch and iOS13.4

y25zhao avatar Dec 28 '20 09:12 y25zhao

This will fix auto rotate : CameraManager properties:

cameraManager.shouldRespondToOrientationChanges = true   
cameraManager.shouldKeepViewAtOrientationChanges = true

UIImage extension:

extension UIImage {
    func fixOrientation() -> UIImage? {
        guard imageOrientation != .up else {
            return self
        }
        UIGraphicsBeginImageContext(size)
        draw(in: CGRect(origin: .zero, size: size))
        let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return normalizedImage
    }
}

Reference : https://stackoverflow.com/a/39595508

Mohit-Allay avatar Mar 05 '21 12:03 Mohit-Allay