2013-01-09 119 views
2

在我的應用程序中,我想將圖像上傳到服務器,並檢索上傳圖像的URL以將其顯示在應用程序中。我發送圖像的NSData。這是我的代碼:在服務器上傳圖像,接收旋轉後的圖像

AsynchronousImageView *imgVw_User = (AsynchronousImageView*)[self.view viewWithTag:K_TagImageViewUser]; 
UIImage *UserImgForBioData = imgVw_User.image; 
UserImgForBioData = [UIImage imageWithCGImage:UserImgForBioData.CGImage scale:1.0f orientation:UserImgForBioData.imageOrientation]; 
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(UserImgForBioData,0.5)]; 

Base64Transcoder *temp_transcoder=[[Base64Transcoder alloc]init]; 
NSString * temp_base64EncodedImage= @""; 
if (imageData!=nil) 
    { 
     temp_base64EncodedImage = [NSString stringWithFormat:@"%@",[temp_transcoder base64EncodedStringfromData:imageData]]; 
     temp_base64EncodedImage = [[temp_base64EncodedImage stringByReplacingOccurrencesOfString:@"\n" 
              withString:@""] 
             mutableCopy]; 
     } 

有時當我顯示圖像時,通過獲取圖像的url,圖像被旋轉。難道我做錯了什麼?

+1

你從相機捕獲圖像? –

+0

問題出現在服務器上的可能性很大,可能不考慮圖像方向標誌,並將它們存儲在旋轉的服務器上。 –

+0

@SatishKAzad - 是的。當我使用相機中的圖像,甚至是從相機點擊的圖像庫時,問題就出現了。 – NiKKi

回答

6

如果[R從攝像機捕捉圖像,然後上傳圖像通過該生成圖像縮略圖在原來的方向前:在此之後

- (UIImage *)generatePhotoThumbnail:(UIImage *)image 
{ 
    //int kMaxResolution = 320; 

    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 = bounds.size.width/ratio; 
    } 
    else 
    { 
    bounds.size.height = kMaxResolution; 
    bounds.size.width = 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"]; 
      break; 
    } 

    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; 

} 

u得到正確的圖像,然後上傳圖片。我希望這會解決你的問題。

第1步:從相機捕捉圖像。

第二步:然後上傳所得到的圖像:通過以下方法

第三步生成photoThumbnail。

使用從照片庫中的圖像時,你會得到正確的圖像

+1

謝謝你。代碼的工作就像一個魅力。爲了參考其他人,我會再次在這裏寫上述評論::從相機捕獲圖像後,通過該圖像生成photumbumbnail,然後你會得到一個實際的圖像。對於照片縮略圖,我已經在下面給你提供了一種方法。實際上原始圖像的方向不正確,下面的方法會調整圖像的實際方向。第1步:從相機捕捉圖像。第2步:通過以下方法生成photoThumbnail第3步:然後上傳合成圖像。你會得到正確的圖像 – NiKKi

+0

歡迎.... :) –

+0

謝謝!完美的作品!我更新了Swift的代碼https://gist.github.com/dc7d8c90195bbeab3e32.git – Madman

0

這爲我工作。

ALAssetRepresentation *rep = [_asset defaultRepresentation]; 
CGImageRef iref = [rep fullResolutionImage]; 
UIImage* img = [UIImage imageWithCGImage:iref scale:1.f orientation:UIImageOrientationUp] 

注意:_asset是ALAsset的一個實例。