使用UIIImagePicker的CGAffineTransform缩放UIImage并保存到Parse SDK [英] Scaling UIImage with a UIIImagePicker's CGAffineTransform and Saving to Parse SDK

查看:99
本文介绍了使用UIIImagePicker的CGAffineTransform缩放UIImage并保存到Parse SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码缩放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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆