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.