1

我得到一個UIImage與UIImagePickerController。獲取圖像後,我將其設置爲UITableView的背景。圖像看起來和我一樣。但是,如果我將圖像保存到我的服務器,然後再加載圖像。無論我使用UIImagePNGRepresentation還是UIImageJPEGRepresentation進行保存,圖像都會在保存時旋轉(旋轉發生在服務器調用之前)。我在這裏讀到,PNG會旋轉,但JPEG不會。但對我來說,都旋轉90度。任何想法爲什麼jpeg在我的情況不好?UIImagePickerController顯示正確的圖像,直到我保存圖像

更多

我從圖像拾取作爲UIImage的原始圖像。然後我保持一個指向UIImage的指針。一段時間後,我想保存UIImage。所以通常我做UIImagePNGRepresentation(myUIImage)。我使用AFNetworking作爲

if ([self.imageDictionaries count]>0) 
    for (NSDictionary *imgDic in self.imageDictionaries) { 
    [formData appendPartWithFileData:UIImagePNGRepresentation([imgDic objectForKey:@"image"]) 
    name:[imgDic objectForKey:@"name"]//@"image" 
    fileName:[imgDic objectForKey:@"fileName"]//@"image.png" 
    mimeType:[imgDic objectForKey:@"mimeType"]//@"image/png" 
    ]; 
    } 
} 
//[imgDic objectForKey:@"name"] is just a UIImage 

一個imageDictionaries的項目看起來像這樣

NSDictionary *background [email protected]{ 
           @"image":self.backgroundImage, 
           @"name":@"bkgimg", 
           @"fileName":@"bkgimg.png", 
           @"mimeType":@"image/png" 
           }; 

另外請注意,我的代碼工作正常。唯一的問題是圖像旋轉。 self.backgroundImage是一個指針的UIImage,我從得到的UIImagePickerController作爲

UIImage *background = info[UIImagePickerControllerOriginalImage]; 

圖像不旋轉,如果我是使用UIImagePickerControllerEditedImage。但這是另一回事。

+0

你還沒有給我足夠的耐心。什麼是'self.imageDictionaries'? – matt 2014-09-26 22:52:38

+0

這是一個字典,其中每個項目是一個字典,其中包含:UIImage,imageName的NSString,fileName的NSString,mimeType的NSString。每行附近的註釋也會顯示。我將在代碼中包含一個示例項目。所以再次imageDictionaries是字典的詞典。 – 2014-09-26 22:58:23

回答

0

不要用UIImagePNGRepresentation或UIImageJPEGRepresentation保存它。使用ImageIO框架。這使您可以保存正確的旋轉信息。

另外,根據你在做什麼(你沒有解釋你是如何得到這些圖像的),通過CGImage可能就足夠了。您可以將旋轉信息附加到UIImage上。

+0

謝謝。我從來沒有聽說過ImageIO框架。我會搜索它。 – 2014-09-26 21:50:51

+0

這是我的(簡短)介紹它:http://www.apeth.com/iOSBook/ch36​​.html#_image_file_formats – matt 2014-09-26 21:55:17

+0

我已更新,以顯示我是一個UIImage作爲給定。你介意如何在沒有png/jpeg表示調用的情況下將該UIImage傳遞給網絡連接?我已經添加了一些代碼 – 2014-09-26 22:03:26

0

圖像旋轉是因爲您將圖像保存爲JPEG格式,如果將圖像保存爲PNG格式,則方向不會改變。這裏是解決方向問題的代碼。

- (UIImage *)fixrotation:(UIImage *)image{ 


if (image.imageOrientation == UIImageOrientationUp) return image; 
CGAffineTransform transform = CGAffineTransformIdentity; 

switch (image.imageOrientation) { 
    case UIImageOrientationDown: 
    case UIImageOrientationDownMirrored: 
     transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height); 
     transform = CGAffineTransformRotate(transform, M_PI); 
     break; 

    case UIImageOrientationLeft: 
    case UIImageOrientationLeftMirrored: 
     transform = CGAffineTransformTranslate(transform, image.size.width, 0); 
     transform = CGAffineTransformRotate(transform, M_PI_2); 
     break; 

    case UIImageOrientationRight: 
    case UIImageOrientationRightMirrored: 
     transform = CGAffineTransformTranslate(transform, 0, image.size.height); 
     transform = CGAffineTransformRotate(transform, -M_PI_2); 
     break; 
    case UIImageOrientationUp: 
    case UIImageOrientationUpMirrored: 
     break; 
} 

switch (image.imageOrientation) { 
    case UIImageOrientationUpMirrored: 
    case UIImageOrientationDownMirrored: 
     transform = CGAffineTransformTranslate(transform, image.size.width, 0); 
     transform = CGAffineTransformScale(transform, -1, 1); 
     break; 

    case UIImageOrientationLeftMirrored: 
    case UIImageOrientationRightMirrored: 
     transform = CGAffineTransformTranslate(transform, image.size.height, 0); 
     transform = CGAffineTransformScale(transform, -1, 1); 
     break; 
    case UIImageOrientationUp: 
    case UIImageOrientationDown: 
    case UIImageOrientationLeft: 
    case UIImageOrientationRight: 
     break; 
} 

// Now we draw the underlying CGImage into a new context, applying the transform 
// calculated above. 
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height, 
             CGImageGetBitsPerComponent(image.CGImage), 0, 
             CGImageGetColorSpace(image.CGImage), 
             CGImageGetBitmapInfo(image.CGImage)); 
CGContextConcatCTM(ctx, transform); 
switch (image.imageOrientation) { 
    case UIImageOrientationLeft: 
    case UIImageOrientationLeftMirrored: 
    case UIImageOrientationRight: 
    case UIImageOrientationRightMirrored: 
     // Grr... 
     CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage); 
     break; 

    default: 
     CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage); 
     break; 
} 

// And now we just create a new UIImage from the drawing context 
CGImageRef cgimg = CGBitmapContextCreateImage(ctx); 
UIImage *img = [UIImage imageWithCGImage:cgimg]; 
CGContextRelease(ctx); 
CGImageRelease(cgimg); 
return img; 
}