How to orient uploaded image?

ios
image

#1

I’m posting a tweet from iOS without the tweet sheet by uploading the image via: https://upload.twitter.com/1.1/media/upload.json

then posting a status with the media_ids argument via: https://api.twitter.com/1.1/statuses/update.json

The tweet posts with the picture, but it is always displayed on Twitter sideways, unless the picture was taken in landscape mode. Are there other parameters I can use to properly display a portrait mode image?

I tried rotating the image data before uploading with this code:

UIImage *image = [UIImage imageWithCGImage:lowRes.CGImage scale:1 orientation:UIImageOrientationRight];

but none of the orientation options have any effect. How can I get the photo to post like this: https://twitter.com/xavierrojas_/status/711428546145550336 ?


#2

Hey @iioinc Can you share links to the posted tweets that are in the incorrect format?


#3

I can’t leave an incorrect post on this account, but here’s a screenshot


#4

Thanks much and just to confirm are you using Twitter Kit 2.0.1 or an earlier version?


#5

I was using 1.15.3, but I updated to 2.0.1 and the same thing happened.


#6

Thanks, I chatted with the team about this and the image would need to be oriented how you would want it to be, prior to uploading. Using something like this would help:

    UIGraphicsBeginImageContext(size);
    CGContextRotateCTM(ctx, M_PI_2);
    [image drawInRect:rect];
    UIImage *output = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

#8

Thanks for the tip, I was able to fix it for all orientations. I think this EXIF image orientation problem was the source of the issue: http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/

Which would make sense because the code I experimentally derived to fix it doesn’t make a lot of sense:

    // prevents hitting upload file size limit (~3MB)
    NSData *lowerResData = UIImageJPEGRepresentation(initialImage, 0.3);
    UIImage *reducedImage = [UIImage imageWithData:lowerResData];
    
    // correct image orientation in select cases
    switch (reducedImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored: {
            NSLog(@"[CameraVC] Rotate photo for Twitter");
            CGSize size = reducedImage.size;
            UIGraphicsBeginImageContext(size);
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextTranslateCTM(context, 0.5f * size.width, 0.5f * size.height);
            [reducedImage drawInRect:(CGRect){ { -size.width * 0.5f, -size.height * 0.5f }, size }];
            reducedImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            break;
        }
        default: break;
    }

I’ve seen a few sporadic 400 errors still when taking photos in UIImageOrientationUp, probably because the upload is too large. The first line could be improved to resize the photo based on its actual size and the actual tweet photo size limit.


#9

Great to hear you’re all set @iioinc and thanks for sharing more details on this. Happy coding!


#10