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

Orientation Issue #44

Closed
ghousesgb opened this issue Jun 21, 2014 · 8 comments
Closed

Orientation Issue #44

ghousesgb opened this issue Jun 21, 2014 · 8 comments

Comments

@ghousesgb
Copy link

Here is the problem I have. I take a picture on my iPhone 5 on the camera and email it to myself. If i go to upload it to a blog or facebook it is always turned sideways. I have to open it on preview and re-save it so it has the correct orientation (even though i did not make any changes).
where as landscape images are fine.

@fulldecent
Copy link
Owner

Hmm when you access the email is it correct then? I'm trying to follow you rtwo-step process.

And what device and software and app?

@ghousesgb
Copy link
Author

No, in mail also, its sturned sideways,

I will provide you the steps.

Take a photo using default camera app and another with FDTake in Portrait mode (I mean home button down).
Now, open photos app select both images and mail yourself.
You will find, photo with default camera will be proper, but image with FDTake will be turns Left side.

Sorry to say that, hope you will come up with solution
Or
I am doing it anything wrong

@ghousesgb
Copy link
Author

following is the delegate code,

  • (void)takeController:(FDTakeController *)controller gotPhoto:(UIImage *)photo withInfo:(NSDictionary *)info {

    if([info objectForKey:@"UIImagePickerControllerMediaMetadata"]!=nil) {
    UIImageWriteToSavedPhotosAlbum(photo,
    self,
    @selector(image:finishedSavingWithError:contextInfo:),
    nil);

    }
    }

@ghousesgb
Copy link
Author

#35 might be pointing to same issue

@fulldecent
Copy link
Owner

Thank you for reporting. I am looking into this and not exactly sure what to do yet.

@ghousesgb
Copy link
Author

Thanks William Entriken, For awesome FDTake,
I have fixed the issue with FixOrientation method which check the current image orientation and rotate it to our needs.
Thanks again.

@fulldecent
Copy link
Owner

Thank you for the update. Would you be able to share this method in case anyone else needs it?

@ghousesgb
Copy link
Author

Call fixOrientation method before UIImageWriteToSavedPhotosAlbum

- (UIImage *)fixOrientation:(UIImage *)image {

    // No-op if the orientation is already correct
    if (image.imageOrientation == UIImageOrientationUp) return image;

    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    CGAffineTransform transform = CGAffineTransformIdentity;

    switch (image.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;

        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, image.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationUpMirrored:
            break;
    }

    switch (image.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;

        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationLeft:
        case UIImageOrientationRight:
            break;
    }

    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
                                             CGImageGetBitsPerComponent(image.CGImage), 0,
                                             CGImageGetColorSpace(image.CGImage),
                                             CGImageGetBitmapInfo(image.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (image.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
            break;

        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);
            break;
    }

    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}

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

No branches or pull requests

2 participants