2012-04-16 3 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에서 동일한 이미지가 회전 된 것처럼 보입니다. 이전에 비슷한 문제가 발생했습니다. –

답변

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

생명의 은인 ... 감사합니다! –

+0

Welcome @BuyinBrian – freelancer

+0

안녕하세요. 이미지를 서버에 업로드 할 때 동일한 방향 문제가 있습니다. 코드가 내 서버에서 내 방향 문제를 해결합니다. – elliptic00

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

는 90 회전 각도가 없습니다 내 코드? 90 0.9이어야한다처럼 읽는 문서

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

편집 같습니다. 문제는 아닐 수도 있습니다.

+0

답장을 보내 주셔서 감사합니다. 0.9 – freelancer

+0

로 변경하지 마십시오. 동일한 결과가 – freelancer

+5

-1로 변경됩니다. 멋쟁이! 90은 JPEG 품질입니다. :) –

1

미디어 업로드에 대한 좋은 쿼리입니다.

장치에서 이미지를 캡처하면 특정 이미지의 방향이 고정됩니다. 이미지를 가로 모드 &으로 캡처하면 이미지가 세로보기로 표시됩니다. 기본 장치 구성은 이미지 지원 (가로 &)을 모두 수행하므로 세로로 가장 할 수 있기 때문에 세로 방향으로 표시됩니다. 실제로 가로로 저장되는 동안 . 결과적으로 사용자가 UI (imageView)에 세로로 이미지를 표시하는 동안 가로로 서버를 절약 할 수 있습니다. 마지막으로 1) 이미지가 장치에 가로로 기록되었습니다. 2) 애플리케이션의 세로 모드로 인해 UI에서 세로로 표시했습니다. 3) 그러나 서버에 쓰여질 때 원래 모드 (수평)를 취합니다.

최상의 제안에 따라 응용 프로그램에서 적절한 모드로 미디어를 표시해야합니다.

2

방향 (사용자의 경우 회전)은 이미지의 exif 데이터에 있습니다. iOS에서 이미지를 미리 회전하거나 서버에서 사후 회전해야합니다.

iOS에서 속성이 UIImage인지 확인할 수 있습니다.

당신은이 냈다 그 코드 변환을 얻을 수 있습니다 (당신이 크기를 설정하거나 코드 부분을 변경해야하는 크기가 원래 크기 일 수 있습니다.) : 물론

- (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에. 하지만 난 당신 stackoverflow에 대한 다른 질문에서 찾을 것 같아요. 어쩌면 here을 시작하십시오.