【问题标题】:Scaling UIImage with a UIIImagePicker's CGAffineTransform and Saving to Parse SDK使用 UIIImagePicker 的 CGAffineTransform 缩放 UIImage 并保存到 Parse SDK
【发布时间】:2014-07-09 06:38:09
【问题描述】:

我使用以下代码来扩展我的UIImagePickerController

    CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0); //This slots the preview exactly in the middle of the screen by moving it down 71 points
    self.imagePicker.cameraViewTransform = translate;
    CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
    self.imagePicker.cameraViewTransform = scale;

然后我拍照并在UIImageView 中展示它

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [picker dismissViewControllerAnimated:NO completion:NULL];

    _imageTaken = nil;
    _imageTaken = [info objectForKey:UIImagePickerControllerEditedImage];
    if(_imageTaken==nil)
    {
        _imageTaken = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    if(_imageTaken==nil)
    {
        _imageTaken = [info objectForKey:UIImagePickerControllerCropRect];
    }

    [_imageTakenView setContentMode:UIViewContentModeScaleAspectFill];

    if (_selfie) {
        UIImage * flippedImage = [UIImage imageWithCGImage:_imageTaken.CGImage scale:_imageTaken.scale orientation:UIImageOrientationLeftMirrored];
        _imageTaken = flippedImage;
    }

    _imageTakenView.image = _imageTaken;
}

到目前为止一切都很好。然后我通过将图像转换为NSData 将图像发送到我的数据库

    _imageData = [[NSData alloc] init];
    _imageData = UIImageJPEGRepresentation(_image, .1);

当我从服务器加载数据并以另一个相同大小的UIImageView 呈现时,我确实设置了相同的纵横比:

    [_imageViewToShow setContentMode:UIViewContentModeScaleAspectFill];

但图像看起来失真(水平挤压)。关于为什么会这样的任何想法?

将 UIImage 转换为 NSData 并保存

_imageData = UIImageJPEGRepresentation(_image, .1);
PFFile *imageFile = [PFFile fileWithName:@"image.png" data:_imageData];
newMessage[@"image"] = imageFile;

问题:

当我重新下载数据时,UIImage 似乎被压扁了。当我查看数据库中的图像时,它似乎很好。不知道为什么会这样……

前后对比图:

【问题讨论】:

  • 您是否在图像视图上设置转换?标题提到了它,但示例代码只显示了图像选择器上的转换。
  • @Austin 我正在图像选择器上设置转换。对不起
  • 您的服务器是否正确保存了所有 JPEG 的元数据?听起来imageOrientation 属性没有被存储,或者图像的高度/宽度被交换了。
  • @Austin 我认为这很有可能,因为我现在正在查看服务器上的图像。我将如何旋转它们/为什么你认为高度/宽度被交换了?
  • 我不知道为什么他们不会得救。我只知道UIImageJPEGRepresentation()将旋转存储为元数据,而不是旋转图像数据本身。

标签: ios objective-c uiimageview uiimage


【解决方案1】:

在上传之前尝试标准化图像的方向。这样即使元数据丢失,图像也能正常工作

这里有一个类别可以做到这一点:

UIImage+OrientationFix.h

@interface UIImage (OrientationFix)

- (UIImage*)imageByNormalizingOrientation;

@end

UIImage+OrientationFix.m

@implementation UIImage (OrientationFix)

- (UIImage*)imageByNormalizingOrientation {
    if (self.imageOrientation == UIImageOrientationUp)
        return self;

    CGSize size = self.size;
    UIGraphicsBeginImageContextWithOptions(size, NO, self.scale);
    [self drawInRect:(CGRect){{0, 0}, size}];
    UIImage* normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return normalizedImage;
}

@end

您只需拨打电话:

_imageTaken = [flippedImage normalizedImage];

【讨论】:

  • 哇。那确实奏效了。你能解释一下这里发生了什么吗?显然,非常感谢。
  • 基本上,图像不仅存储像素,还需要存储方向信息。您创建的UIImage 具有UIImageOrientationLeftMirroredUIImageView 使用此信息正确显示它。但是,当您转换为 JPEG 时,它们有自己的方式来存储旋转信息。您可以查看stackoverflow.com/questions/5125323/… 了解更多详情。但是这个修复基本上使方向“向上”,这样就没有人会在显示它时遇到问题。
  • 太棒了。谢谢杰克。一旦允许,我会奖励你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-01
  • 2012-11-22
相关资源
最近更新 更多