使用 UIIImagePicker 的 CGAffineTransform 缩放 UIImage 并保存到 Parse SDK

Scaling UIImage with a UIIImagePicker#39;s CGAffineTransform and Saving to Parse SDK(使用 UIIImagePicker 的 CGAffineTransform 缩放 UIImage 并保存到 Parse SDK)
本文介绍了使用 UIIImagePicker 的 CGAffineTransform 缩放 UIImage 并保存到 Parse SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码来缩放我的 UIImagePickerController.

I use the following code to scale my 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

Then I take the picture and present it in a 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

Everything is good so far. Then I send the image up to my database by converting it to NSData

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

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

When I load the data back from the server and present it in another UIImageView of the same size I do set the same aspect ratio:

    [_imageViewToShow setContentMode:UIViewContentModeScaleAspectFill];

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

but the image looks distored (squished horizontally). Any ideas as to why this might be?

将 UIImage 转换为 NSData &保存

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

问题:

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

When I redownload the data, the UIImage appears squished. When I look at the image on my database, it seems fine. Not sure why this is happening...

前后对比图:

推荐答案

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

Try normalizing the image's orientation before uploading.This will allow the image to work properly even if the metadata is lost

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

Here is a category to do that:

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

这篇关于使用 UIIImagePicker 的 CGAffineTransform 缩放 UIImage 并保存到 Parse SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)