2011-11-24 4 views
2

견적 양식을 나타내는 클래스가 있습니다. 사진을 찍은 다음 앱에서 첨부 파일로 추가하는 버튼을 추가했습니다. 내 imageView에서는 사진이 세로 방향으로 표시되지만 이메일 첨부 파일에서는 90 ° 회전되어 가로 방향으로 표시됩니다. 이 문제를 어떻게 해결할 수 있습니까?카메라에서 전자 메일 첨부 파일이 세로 방향으로 계속 회전합니다.

+1

이미지는 장치의 방위에 기초하여 imageOrientation 속성 집합을 해결한다. 많은 앱이이 속성을 지원하지 않으므로 이미지가 회전 된 것처럼 보입니다. 이미지를 촬영 한 후 이미지를 회전하여이 문제를 해결하십시오. UIImagePickerController로 찍은 이미지를 회전/방향 지정하기 위해 SO를 검색하십시오. 여기에이 주제에 대한 많은 도움이 있습니다. – XJones

+0

앱에서 사용할 수있는 유일한보기는 세로이므로 다른 항목이 표시된다는 것에 놀랐습니다. 귀하의 기준에 따라 검색하겠습니다 ... 감사합니다 – fmi

+0

앱의 방향은 중요하지 않습니다. 'UIImagePickerController'를 사용하여 사진을 찍을 때, 사진을 찍을 때 장치를 잡는 방법에 따라'imageOrientation'이 설정됩니다. – XJones

답변

2

또한 동일한 문제를 직면하지만,이 코드는 사진을 촬영할 때 iOS의 카메라로 촬영 된 것이

- (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; 
} 
+0

도움을 주셔서 감사합니다,하지만 그것은 나를 위해 밖으로 작동하지 않습니다. 내 이미지는 첨부 파일에서 여전히 가로 방향임을 나타냅니다. 변수 중 하나를 변경해야합니까? – fmi

+0

원본 이미지의 크기를 변경할 수 있습니다. –

관련 문제