15

我目前有一个使用 ALAsssetsLibrary 来获取照片的应用程序。我已将照片放置到图像视图中,并且可以上传到服务器。拍了一些照片后在真机上测试时,我发现本来应该在人像中拍摄的照片变成了风景。

因此,我调用了不同的函数来获得这样的 CGImage:

UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:1.0 orientation:(UIImageOrientation)[representation orientation]];

第一次尝试,我用这个:

UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage]]

我认为具有比例和方向的那个可以给我正确的方向来拍摄照片。但它没有给我正确的解决方案。

我是否错过了生成正确照片方向所需的任何内容?

4

4 回答 4

45

正确的方向处理取决于您使用的 iOS 版本。在 iOS4 和 iOS 5 上,缩略图已经正确旋转,因此您可以在不指定任何旋转参数的情况下初始化 UIImage。但是对于 fullScreenImage,每个 iOS 版本的行为都不同。在 iOS 5 上,图像已经在 iOS 4 上没有旋转。

所以在 iOS4 上你应该使用:

ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
UIImage *_image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] 
                                                     scale:[defaultRep scale] orientation:(UIImageOrientation)[defaultRep orientation]];

在 iOS5 上,以下代码应该可以正常工作:

ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
UIImage *_image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0];

干杯,

亨德里克

于 2012-02-25T19:48:39.723 回答
4

试试这个代码: -

 UIImage* img = [UIImage imageWithCGImage:asset.thumbnail];
 img = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationUp];

这可能会对您有所帮助。

于 2012-01-12T09:52:46.520 回答
3

我的经验仅限于 IOS 5.x,但我可以告诉您缩略图和全屏图像的方向正确。垂直拍摄时,它是水平的全分辨率图像。我的解决方案是使用从这里获得的 uiimage 类别:

http://www.catamount.com/forums/viewtopic.php?f=21&t=967&start=0

它在 UIImage 上提供了一个很好的旋转方法,如下所示:

        UIImage *tmp = [UIImage imageWithCGImage:startingFullResolutionImage];
        startingFullResolutionImage = [[tmp imageRotatedByDegrees:-90.0f] CGImage];
于 2012-03-29T20:09:47.153 回答
0

对于fullResolutionImage,我想提供如下解决方案,

ALAssetRepresentation *rep = [asset defaultRepresentation];

// First, write orientation to UIImage, i.e., EXIF message.
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage] scale:rep.scale orientation:(UIImageOrientation)rep.orientation];
// Second, fix orientation, and drop out EXIF
if (image.imageOrientation != UIImageOrientationUp) {
   UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
   [image drawInRect:(CGRect){0, 0, image.size}];
   UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   image = normalizedImage;
}
// Third, compression
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

imageData是您想要的,只需将其上传到您的照片服务器。

顺便说一句,如果您认为 EXIF 有用,您可以根据需要对其进行补充normalizedImage

于 2016-02-19T09:19:10.383 回答