【问题标题】:email attachment from camera keeps rotating to landscape from portrait来自相机的电子邮件附件不断从纵向旋转到横向
【发布时间】:2011-11-24 01:26:04
【问题描述】:

我有一个代表报价单的类。我添加了一个按钮来拍照,然后从应用程序中将其添加为附件。在我的 imageView 中,图片显示为纵向(因为它是这样拍摄的),但在电子邮件附件中,它被旋转了 90°,因此它是横向的。我该如何解决这个问题?

【问题讨论】:

  • 使用 iOS 相机拍摄的图像有一个 imageOrientation 属性,该属性根据拍摄照片时设备的方向设置。许多应用程序不支持此属性,因此图像看起来是旋转的。您可以通过在拍摄后旋转图像来纠正此问题。在 SO 上搜索使用 UIImagePickerController 拍摄的旋转/定向图像。这里有很多关于这个主题的帮助。
  • 应用程序中唯一可用的视图是纵向,所以我很惊讶它会显示其他任何内容。我会根据您的条件进行搜索...谢谢
  • 应用程序的方向无关紧要。当您使用UIImagePickerController 拍照时,imageOrientation 是根据您在拍照时握持设备的方式设置的。
  • @XJones 我拿着设备肖像,imageView 显示肖像……但它在我的电子邮件附件中被旋转(横向)。
  • 我试图用imageOrientation 属性为您指明正确的方向。这是有关此主题的众多 SO 问题之一。答案看起来不错。 stackoverflow.com/questions/5427656/…

标签: ios email camera attachment


【解决方案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; 
}

【讨论】:

  • 感谢您的帮助,但它不适合我。我的图像显示它仍然是附件中的风景。我需要更改其中一个变量吗?
  • 你可以改变原图的大小
猜你喜欢
  • 2012-09-20
  • 1970-01-01
  • 2023-03-05
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
  • 2012-04-04
  • 2012-01-08
  • 1970-01-01
相关资源
最近更新 更多