Scaling UIImage with the UIIImagePicker CGAffineTransform and Saving to Parse SDK

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;

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

So far, all is well. Then I send the image to my database, converting it toNSData

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

When I download data from the server and present it in another of UIImageViewthe same size, I set the same aspect ratio:

    [_imageViewToShow setContentMode:UIViewContentModeScaleAspectFill];

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

Convert UIImage to NSData and save

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

Problem:

When I reload the data, the UIImage seems wrinkled. When I look at an image in my database, it seems perfect. I don’t know why this is happening ...

Images before and after:

enter image description here

enter image description here

+4
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];
+5

Source: https://habr.com/ru/post/1541337/


All Articles