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; 
}