Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScreenShot Func Question #5

Closed
SuYungCheng opened this issue Apr 17, 2016 · 5 comments
Closed

ScreenShot Func Question #5

SuYungCheng opened this issue Apr 17, 2016 · 5 comments
Labels

Comments

@SuYungCheng
Copy link

I take a screenshot from my video.
If the video is Portrait,after screenshot, my image show Landscape,
If the video is Landscape , and the image is the same with Video.

Could you please teach me how to fix it?

Wring Direction Pic
Correct Direction Pic

PS.
I use the function to fix my img from screenshot.
But it does not work.
Thank you!

extension UIImage {

  func fixOrientation() -> UIImage {
    if (self.imageOrientation == .Up) {
      return self
    }

    var transform = CGAffineTransformIdentity

    switch (self.imageOrientation) {
    case .Down, .DownMirrored:
      transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height)
      transform = CGAffineTransformRotate(transform, CGFloat(M_PI))

    case .Left, .LeftMirrored:
      transform = CGAffineTransformTranslate(transform, self.size.width, 0)
      transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))

    case .Right, .RightMirrored:
      transform = CGAffineTransformTranslate(transform, 0, self.size.height)
      transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))

    default:
      break
    }

    switch (self.imageOrientation) {
    case .UpMirrored, .DownMirrored:
      transform = CGAffineTransformTranslate(transform, self.size.width, 0)
      transform = CGAffineTransformScale(transform, -1, 1)

    case .LeftMirrored, .RightMirrored:
      transform = CGAffineTransformTranslate(transform, self.size.height, 0)
      transform = CGAffineTransformScale(transform, -1, 1)

    default:
      break
    }

    let ctx = CGBitmapContextCreate(nil, Int(self.size.width), Int(self.size.height),
      CGImageGetBitsPerComponent(self.CGImage), 0,
      CGImageGetColorSpace(self.CGImage),
      CGImageGetBitmapInfo(self.CGImage).rawValue)
    CGContextConcatCTM(ctx, transform)

    switch (self.imageOrientation) {
    case .Left, .LeftMirrored, .Right, .RightMirrored:
      CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage)

    default:
      CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage)
    }

    // And now we just create a new UIImage from the drawing context
    let cgimg = CGBitmapContextCreateImage(ctx)
    return UIImage(CGImage: cgimg!)
  }
}

let fixedImage = image.fixOrientation()

@davidlondono
Copy link
Owner

it seams the image always go to portrait, im gonna be making some tests to try to fix it

@SuYungCheng
Copy link
Author

Very pleased to know that , and hope to hear your reply of it.

@davidlondono
Copy link
Owner

ok the Pod has been updated and solves your problem, tell me about it

@SuYungCheng
Copy link
Author

Thank you so much. I'll try it as soon as possible.

@SuYungCheng
Copy link
Author

SuYungCheng commented May 24, 2016

I had try the new Pod and it really solves my problem.It's really amazing!Let me know if you need more info.Thanks in advance.
Sincerely!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants