2012-04-16 61 views
1

我使用圖像選擇器捕獲圖像,此時圖像處於其實際位置。但是在將其上傳到服務器之後,它變得水平。我不明白爲什麼會發生這種情況。使用Http Post成功上傳到服務器後,圖像變爲水平

這裏是我的代碼

NSString *urlString = @"Url"; 
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 

    NSMutableData *body = [NSMutableData data]; 


    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"]; 

    // file 
    NSData *imageData = UIImageJPEGRepresentation(image.image, 90); 

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    // [body appendData:[[NSString stringWithString:@"Content-Disposition: attachment; name=\"user_photo\"; filename=\"photoes.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@.jpg\"\r\n",@"ImageNmae"] dataUsingEncoding:NSUTF8StringEncoding]]; 

    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:imageData]]; 
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 




    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"desc\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:ImageDesc.text] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

    // close form 
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    // set request body 
    [request setHTTPBody:body]; 

    //return and test 
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
+0

這是MAC/WINDOWS操作系統讀取不同的問題。如果您在MAC上看到該照片,則只會在您上傳時看到其方向,而相同的圖像將在WINDOWS上顯示爲旋轉狀態。我們以前遇到過類似的問題。 – 2012-04-16 07:40:20

回答

14

我覺得這裏的解決方案是代碼。

- (UIImage *)scaleAndRotateImage:(UIImage *)image { // here we rotate the image in its orignel 
int kMaxResolution = 640; // Or whatever 

CGImageRef imgRef = image.CGImage; 

CGFloat width = CGImageGetWidth(imgRef); 
CGFloat height = CGImageGetHeight(imgRef); 


CGAffineTransform transform = CGAffineTransformIdentity; 
CGRect bounds = CGRectMake(0, 0, width, height); 
if (width > kMaxResolution || height > kMaxResolution) { 
    CGFloat ratio = width/height; 
    if (ratio > 1) { 
     bounds.size.width = kMaxResolution; 
     bounds.size.height = roundf(bounds.size.width/ratio); 
    } 
    else { 
     bounds.size.height = kMaxResolution; 
     bounds.size.width = roundf(bounds.size.height * ratio); 
    } 
} 

CGFloat scaleRatio = bounds.size.width/width; 
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); 
CGFloat boundHeight; 
UIImageOrientation orient = image.imageOrientation; 
switch(orient) { 

    case UIImageOrientationUp: //EXIF = 1 
     transform = CGAffineTransformIdentity; 
     break; 

    case UIImageOrientationUpMirrored: //EXIF = 2 
     transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); 
     transform = CGAffineTransformScale(transform, -1.0, 1.0); 
     break; 

    case UIImageOrientationDown: //EXIF = 3 
     transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height); 
     transform = CGAffineTransformRotate(transform, M_PI); 
     break; 

    case UIImageOrientationDownMirrored: //EXIF = 4 
     transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); 
     transform = CGAffineTransformScale(transform, 1.0, -1.0); 
     break; 

    case UIImageOrientationLeftMirrored: //EXIF = 5 
     boundHeight = bounds.size.height; 
     bounds.size.height = bounds.size.width; 
     bounds.size.width = boundHeight; 
     transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width); 
     transform = CGAffineTransformScale(transform, -1.0, 1.0); 
     transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
     break; 

    case UIImageOrientationLeft: //EXIF = 6 
     boundHeight = bounds.size.height; 
     bounds.size.height = bounds.size.width; 
     bounds.size.width = boundHeight; 
     transform = CGAffineTransformMakeTranslation(0.0, imageSize.width); 
     transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
     break; 

    case UIImageOrientationRightMirrored: //EXIF = 7 
     boundHeight = bounds.size.height; 
     bounds.size.height = bounds.size.width; 
     bounds.size.width = boundHeight; 
     transform = CGAffineTransformMakeScale(-1.0, 1.0); 
     transform = CGAffineTransformRotate(transform, M_PI/2.0); 
     break; 

    case UIImageOrientationRight: //EXIF = 8 
     boundHeight = bounds.size.height; 
     bounds.size.height = bounds.size.width; 
     bounds.size.width = boundHeight; 
     transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0); 
     transform = CGAffineTransformRotate(transform, M_PI/2.0); 
     break; 

    default: 
     [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; 

} 

UIGraphicsBeginImageContext(bounds.size); 

CGContextRef context = UIGraphicsGetCurrentContext(); 

if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { 
    CGContextScaleCTM(context, -scaleRatio, scaleRatio); 
    CGContextTranslateCTM(context, -height, 0); 
} 
else { 
    CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
    CGContextTranslateCTM(context, 0, -height); 
} 

CGContextConcatCTM(context, transform); 

CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); 
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return imageCopy; 
} 
+0

救命......謝謝! – 2012-09-21 21:34:39

+0

歡迎您@BuyinBrian – freelancer 2012-09-22 04:34:26

+0

嗨,我有相同的方向問題時,圖像上傳到服務器。該代碼是否會糾正我的服務器上的方向問題。 – elliptic00 2015-08-11 20:57:42

-5
NSData *imageData = UIImageJPEGRepresentation(image.image, 90); 

是不是90的旋轉角度?閱讀文檔

NSData *imageData = UIImageJPEGRepresentation(image.image, 90); 

編輯看起來像90應該是0.9。可能不是問題。

+0

感謝您的回覆讓我檢查0.9 – freelancer 2012-04-16 07:23:50

+0

沒有改變相同的結果 – freelancer 2012-04-16 07:30:40

+5

-1關於那個:夥計! 90是JPEG質量。 :) – 2012-04-16 07:36:31

1

它是一個很好的媒體上傳查詢。

當您從設備捕捉圖像時,它會修正特定圖像的方向。 假設你在橫向模式下捕捉圖像&當你用imageView顯示它時,它會以縱向模式顯示它自身,因爲基本的設備配置同時支持圖像(縱向&肖像),那麼它可能假裝自己是縱向而實際上存儲在橫向。因此,它在橫向上保存在服務器上時在UI(imageView)中以縱向顯示圖像。 終於1)圖像水平寫在設備上。 2)由於縱向模式對應用程序有效,您已在UI上垂直顯示。 3)但是當它被寫在服務器上時,它將採用其原始模式(水平)。

因此,根據最佳建議,您應該在應用程序中以適當的模式顯示媒體。

2

方向(在你的情況下旋轉)在圖像的exif數據中。 您需要預先旋轉iOS上的圖像或在服務器上對其進行後期旋轉。

在iOS上,您可以檢查UIImageimageOrientation財產。

你可以得到這些代碼剪斷變換(你需要設置大小或者更改代碼部分大小可能是原始大小。):

- (CGAffineTransform)transformForOrientation:(CGSize)newSize { 
    CGAffineTransform transform = CGAffineTransformIdentity; 

    switch (self.imageOrientation) { 
     case UIImageOrientationDown:   // EXIF = 3 
     case UIImageOrientationDownMirrored: // EXIF = 4 
      transform = CGAffineTransformTranslate(transform, newSize.width, newSize.height); 
      transform = CGAffineTransformRotate(transform, M_PI); 
      break; 

     case UIImageOrientationLeft:   // EXIF = 6 
     case UIImageOrientationLeftMirrored: // EXIF = 5 
      transform = CGAffineTransformTranslate(transform, newSize.width, 0); 
      transform = CGAffineTransformRotate(transform, M_PI_2); 
      break; 

     case UIImageOrientationRight:   // EXIF = 8 
     case UIImageOrientationRightMirrored: // EXIF = 7 
      transform = CGAffineTransformTranslate(transform, 0, newSize.height); 
      transform = CGAffineTransformRotate(transform, -M_PI_2); 
      break; 
    } 

    switch (self.imageOrientation) { 
     case UIImageOrientationUpMirrored:  // EXIF = 2 
     case UIImageOrientationDownMirrored: // EXIF = 4 
      transform = CGAffineTransformTranslate(transform, newSize.width, 0); 
      transform = CGAffineTransformScale(transform, -1, 1); 
      break; 

     case UIImageOrientationLeftMirrored: // EXIF = 5 
     case UIImageOrientationRightMirrored: // EXIF = 7 
      transform = CGAffineTransformTranslate(transform, newSize.height, 0); 
      transform = CGAffineTransformScale(transform, -1, 1); 
      break; 
    } 

    return transform; 
} 

當然,你需要的是應用轉換以某種方式對你的UIImage。但我想你會發現在其他問題上的計算器。也許開始here

相關問題