How to orient uploaded image?



I’m posting a tweet from iOS without the tweet sheet by uploading the image via:

then posting a status with the media_ids argument via:

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: ?


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


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


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


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


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:

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


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:

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;
            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();
        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.


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