Search code examples
iosobjective-cuiimagensdata

Resized width and height of image is not same when saved in documents directory


I am resizing and saving image to documents directory using these methods

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

    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];

    CGSize reSize = [self imageResizeCalculationWithAspectRatio:chosenImage.size];

    UIImage *resizedImage = [self resizeImage:chosenImage withAspectRatio:reSize];

    [picker dismissViewControllerAnimated:YES completion:NULL];

    UIImageWriteToSavedPhotosAlbum(resizedImage, nil, nil, nil);

    [self save:resizedImage];
}


-(CGSize)imageResizeCalculationWithAspectRatio:(CGSize)originalSize {
    float maxWidth = 2048;
    float maxHeight = 2048;
    float ratio = 0;
    float width = originalSize.width;
    float height = originalSize.height;

    if(width > maxWidth) {
        ratio = maxWidth / width;
        height = height * ratio;
        width = width * ratio;
    }

    if(height > maxHeight) {
        ratio = maxHeight / height;
        width = width * ratio;
        height = height * ratio;
    }

    return CGSizeMake(width, height);
}

- (UIImage *)resizeImage:(UIImage*)image withAspectRatio:(CGSize)ratio {
    CGRect scaledRect = AVMakeRectWithAspectRatioInsideRect(image.size, CGRectMake(0, 0, ratio.width, ratio.height));
    UIGraphicsBeginImageContextWithOptions(ratio, NO, 0);
    [image drawInRect:scaledRect];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

-(void)save:(UIImage*)image {
    NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];
    // New Folder is your folder name
    NSError *error = nil;
    if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

    NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"];
    NSData *data = UIImageJPEGRepresentation(image, 1.0);
    [data writeToFile:fileName atomically:YES];
}

My 11000 x 5000 dimension image A from gallery is resizing by my methods as <UIImage: 0x12ea72490> size {2048, 931} orientation 0 scale 3.000000 BUT saving as 6144 × 2793 dimension and 8.6 mb in size

My 720 x 480 dimension image B from gallery is resizing by my methods as <UIImage: 0x12ea9a360> size {720, 480} orientation 0 scale 3.000000 BUT saving as 2160 × 1440 dimension and 123 kb in size

Why image dimensions are exceeding from dimensions calculated by my methods, I want to save the image in dimensions that are resized.

How can i achieve it?


Solution

  • This is expected behavior. Your resized image has a scale of 3. 3 times 2048 x 931 is 6144 × 2793.

    If you didn't want your resized image to have a scale of 3, why did you say

    UIGraphicsBeginImageContextWithOptions(ratio, NO, 0);
    

    ?? You are running on a triple-resolution screen, so you get a triple-resolution image. If that's not what you wanted, you should have said

    UIGraphicsBeginImageContextWithOptions(ratio, NO, 1);