菜鸟教程小白 发表于 2022-12-11 20:35:13

ios - 来自相机的电子邮件附件不断从纵向旋转到横向


                                            <p><p>我有一个代表报价单的类。我添加了一个按钮来拍照,然后从应用程序中将其添加为附件。在我的 imageView 中,图片显示为纵向(因为它是这样拍摄的),但在电子邮件附件中,它被旋转了 90°,因此它是横向的。我该如何解决这个问题?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我也遇到了同样的问题,但是这段代码解决了它</p>

<pre><code>- (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 &gt; 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 &gt; heightFactor) {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
      }
      else if (widthFactor &lt; heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
      }
    }   

    CGImageRef imageRef = ;
    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 = ;

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




    return newImage;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 来自相机的电子邮件附件不断从纵向旋转到横向,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8251240/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8251240/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 来自相机的电子邮件附件不断从纵向旋转到横向