2011-05-12 48 views
6

我上傳的圖像從iphone點擊從橫向和縱向模式。 橫向模式的圖像上傳正常,但問題是與肖像模式上傳的圖像。他們旋轉了90度。在肖像模式下從iPhone點擊的圖像旋轉90度

其他圖片與肖像模式(不從iPhone單擊)工作正常。

任何想法爲什麼會發生這種情況?

+0

你在哪裏上傳到? – shabbirv 2011-05-12 04:01:09

+0

我將它上傳到我的服務器 – 2011-05-12 04:56:44

+0

實際上圖像已經旋轉,但它在iPhone和Mac上正常顯示。 – 2011-05-16 09:19:06

回答

5

在您的委託:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

後你會得到你的UIImage從「信息」字典鍵「UIImagePickerControllerOriginalImage」,就可以看到imageOrientation財產的形象定位。如果不像你想在上傳之前旋轉你的圖片。

imageOrientation 
The orientation of the receiver’s image. (read-only) 

@property(nonatomic, readonly) UIImageOrientation imageOrientation 
Discussion 
Image orientation affects the way the image data is displayed when drawn. By default, images are displayed in the 「up」 orientation. If the image has associated metadata (such as EXIF information), however, this property contains the orientation indicated by that metadata. For a list of possible values for this property, see 「UIImageOrientation.」 

Availability 
Available in iOS 2.0 and later. 
Declared In 
UIImage.h 

UIImage Class Reference

UIImagePickerController Class Reference

UIImagePickerControllerDelegate Protocol Reference

第二個選項:

是允許用戶編輯您的圖像和獲取圖像 「UIImagePickerControllerEditedImage」;

將您的UIImagePickerControllerallowsEditing」屬性設置爲Yes

在您的代表中,您只需從「info」字典中獲取「UIImagePickerControllerEditedImage」密鑰的UIImage即可。

祝你好運。

+0

這正是我想到的。但我的問題是,當iPhone上沒有點擊上傳的圖片時,一切正常。但只有當它通過iPhone點擊後,它纔會旋轉。那麼與iPhone方向的問題是,當它將圖像轉換爲nsdate時,它將以旋轉的形式提供數據? – 2011-05-12 11:17:51

+0

獲取圖像方向的uiimage測試後,如果方向有誤,並將其轉換爲nsdata,則將其旋轉。 – 2011-05-12 11:56:43

+1

問題是當我上傳一個正常的肖像圖像,然後它工作正常,但是當我上傳從iPhone捕獲的肖像圖像,然後只有它被旋轉。另外如果我在Windows機器上看到這個圖像,它已經旋轉了,但是當我在Mac或iPhone上看到相同的圖像時,它看起來很好。 – 2011-05-12 13:52:04

4

我已經與這個問題摔跤了很久,我正在研究一個需要實際旋轉圖像的項目,就像重新排列像素以便我可以上傳它一樣。

您需要做的第一件事是確定方向,然後去掉那些煩人的元數據,然後旋轉圖像。

於是就把這裏面的didFinishPickingMediaWithInfo功能:

UIImage * img = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

    if ([info objectForKey:@"UIImagePickerControllerMediaMetadata"]) { 
    //Rotate based on orientation 
    switch ([[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] intValue]) { 
     case 3: 
      //Rotate image to the left twice. 
      img = [UIImage imageWithCGImage:[img CGImage]]; //Strip off that pesky meta data! 
      img = [rotateImage rotateImage:[rotateImage rotateImage:img withRotationType:rotateLeft] withRotationType:rotateLeft]; 
      break; 

     case 6: 
      img = [UIImage imageWithCGImage:[img CGImage]]; 
      img = [rotateImage rotateImage:img withRotationType:rotateRight]; 
      break; 

     case 8: 
      img = [UIImage imageWithCGImage:[img CGImage]]; 
      img = [rotateImage rotateImage:img withRotationType:rotateLeft]; 
      break; 

     default: 
      break; 
    } 
} 

這裏是調整大小功能:

+(UIImage*)rotateImage:(UIImage*)image withRotationType:(rotationType)rotation{ 
    CGImageRef imageRef = [image CGImage]; 
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); 
    CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB(); 

    if (alphaInfo == kCGImageAlphaNone) 
     alphaInfo = kCGImageAlphaNoneSkipLast; 

     CGContextRef bitmap; 

    bitmap = CGBitmapContextCreate(NULL, image.size.height, image.size.width, CGImageGetBitsPerComponent(imageRef), 4 * image.size.height/*CGImageGetBytesPerRow(imageRef)*/, colorSpaceInfo, alphaInfo); 
    CGColorSpaceRelease(colorSpaceInfo); 

    if (rotation == rotateLeft) { 
     CGContextTranslateCTM (bitmap, image.size.height, 0); 
     CGContextRotateCTM (bitmap, radians(90)); 
    } 
    else{ 
     CGContextTranslateCTM (bitmap, 0, image.size.width); 
     CGContextRotateCTM (bitmap, radians(-90)); 
    } 

    CGContextDrawImage(bitmap, CGRectMake(0, 0, image.size.width, image.size.height), imageRef); 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
    UIImage *result = [UIImage imageWithCGImage:ref]; 
    CGImageRelease(ref); 
    CGContextRelease(bitmap); 
    return result; 
} 

img變量現在包含一個正確旋轉圖像。

0

好,更清潔的版本是:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *img = [info valueForKey:UIImagePickerControllerOriginalImage]; 
    img = [UIImage imageWithCGImage:[img CGImage]]; 

    UIImageOrientation requiredOrientation = UIImageOrientationUp; 
    switch ([[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] intValue]) 
    { 
     case 3: 
      requiredOrientation = UIImageOrientationDown; 
      break; 
     case 6: 
      requiredOrientation = UIImageOrientationRight; 
      break; 
     case 8: 
      requiredOrientation = UIImageOrientationLeft; 
      break; 
     default: 
      break; 
    } 

    UIImage *portraitImage = [[UIImage alloc] initWithCGImage:img.CGImage scale:1.0 orientation:requiredOrientation]; 

}