gpt4 book ai didi

ios - 使用 UIIImagePicker 的 CGAffineTransform 缩放 UIImage 并保存到解析 SDK

转载 作者:可可西里 更新时间:2023-11-01 05:35:12 26 4
gpt4 key购买 nike

我使用以下代码来缩放我的 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 看起来被压扁了。当我查看数据库中的图像时,它看起来不错。不确定为什么会这样......

前后图像:

enter image description here

enter image description here

最佳答案

在上传之前尝试规范化图像的方向。这将使图像即使在元数据丢失的情况下也能正常工作

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

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

关于ios - 使用 UIIImagePicker 的 CGAffineTransform 缩放 UIImage 并保存到解析 SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23769310/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com