вложение электронной почты с камеры продолжает вращаться в альбомной ориентации из портретной

У меня есть класс, который представляет форму цитаты. Я добавил кнопку, чтобы сделать снимок, а затем добавить его в качестве вложения из приложения. В моем изображении изображение показывает портрет (так как оно было снято таким образом), но во вложении электронной почты оно поворачивается на 90 °, так что оно становится альбомным. Как я могу это исправить?


person fmi    schedule 24.11.2011    source источник
comment
Изображения, снятые с помощью камеры iOS, имеют свойство imageOrientation, установленное на основе ориентации устройства при съемке изображения. Многие приложения не поддерживают это свойство, поэтому изображение выглядит повернутым. Вы исправите это, повернув изображение после того, как оно было сделано. Найдите в SO вращающиеся/ориентирующие изображения, сделанные с помощью UIImagePickerController. Здесь много помощи по этой теме.   -  person XJones    schedule 24.11.2011
comment
Единственный вид, доступный в приложении, — это портрет, поэтому я удивлен, что он показывает что-то еще. Я буду искать по вашим критериям... спасибо   -  person fmi    schedule 24.11.2011
comment
ориентация приложения не имеет значения. когда вы делаете снимок с помощью UIImagePickerController, imageOrientation устанавливается в зависимости от того, как вы держите устройство, когда делаете снимок.   -  person XJones    schedule 24.11.2011
comment
@XJones Я держу портрет устройства, и imageView показывает портрет ... но он повернут (в альбомной ориентации) в моем вложении электронной почты.   -  person fmi    schedule 24.11.2011
comment
Я попытался указать вам правильное направление с помощью свойства imageOrientation. Вот один из многих вопросов SO по этой теме. Ответ выглядит достойно. stackoverflow.com/questions/5427656 /   -  person XJones    schedule 24.11.2011
comment
Другой ответ на этот вопрос вы найдете здесь.   -  person Gallymon    schedule 29.11.2013


Ответы (1)


Я также столкнулся с той же проблемой, но этот код решает ее

- (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSizeWithSameAspectRatio:(CGSize)targetSize
{  
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor > heightFactor) {
            scaleFactor = widthFactor; // scale to fit height
        }
        else {
            scaleFactor = heightFactor; // scale to fit width
        }

        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        // center the image
        if (widthFactor > heightFactor) {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
        }
        else if (widthFactor < heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }     

    CGImageRef imageRef = [sourceImage CGImage];
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

    if (bitmapInfo == kCGImageAlphaNone) {
        bitmapInfo = kCGImageAlphaNoneSkipLast;
    }

    CGContextRef bitmap;

    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
        bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    } else {
        bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    }   

    // In the right or left cases, we need to switch scaledWidth and scaledHeight,
    // and also the thumbnail point
    if (sourceImage.imageOrientation == UIImageOrientationLeft) {
        thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
        CGFloat oldScaledWidth = scaledWidth;
        scaledWidth = scaledHeight;
        scaledHeight = oldScaledWidth;

        CGContextRotateCTM (bitmap, radians(90));
        CGContextTranslateCTM (bitmap, 0, -targetHeight);

    } else if (sourceImage.imageOrientation == UIImageOrientationRight) {
        thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
        CGFloat oldScaledWidth = scaledWidth;
        scaledWidth = scaledHeight;
        scaledHeight = oldScaledWidth;

        CGContextRotateCTM (bitmap, radians(-90));
        CGContextTranslateCTM (bitmap, -targetWidth, 0);

    } else if (sourceImage.imageOrientation == UIImageOrientationUp) {
        // NOTHING
    } else if (sourceImage.imageOrientation == UIImageOrientationDown) {
        CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
        CGContextRotateCTM (bitmap, radians(-180.));
    }

    CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage* newImage = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);
    NSLog(@"sourceImage:%i",sourceImage.imageOrientation);
    NSLog(@"newImage:%i",newImage.imageOrientation);




    return newImage; 
}
person Rahul Chavan    schedule 28.11.2011
comment
Спасибо за помощь, но у меня не получается. Мое изображение показывает, что это все еще пейзаж во вложении. Нужно ли менять одну из переменных? - person fmi; 29.11.2011
comment
Вы можете изменить размер исходного изображения - person Rahul Chavan; 29.11.2011