Image getting bigger after saving it

101 views Asked by At

I would be grateful if anyone could help me to solve an issue with image saving. I've got an app which contains recipes with corresponding images. I need to resize them in order to preserve memory and make my app more robust. Everything looks ok the UIImage has proper dimensions. But when I save this image to Documents it appears to become something like twice bigger than it should be.

The code I use for image resizing:

+(UIImage *) createImageFromImage: (UIImage *)image WithName:(NSString *)name Thumbnail: (BOOL)isThumbnail{
CGSize oldSize = image.size;
CGSize newSize;
CGFloat scaleFactor;
if (isThumbnail) {
    name = [NSString stringWithFormat:@"t_%@", name];
    newSize = CGSizeMake(125, 92);
}
else{
    newSize = CGSizeMake(320, 240);
}

if (!CGSizeEqualToSize(newSize, oldSize)) {
    CGFloat widthFactor = newSize.width/oldSize.width;
    CGFloat heightFactor = newSize.height/oldSize.height;

    if (((widthFactor > heightFactor)||(heightFactor > 1))&&(widthFactor < 1)) {
        scaleFactor = widthFactor;
    }
    else{
        scaleFactor = heightFactor;
    }

    newSize.width = scaleFactor*oldSize.width;
    newSize.height = scaleFactor*oldSize.height;
}

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:name];

UIImage *newImage = nil;
newImage = [self drawImage:image InContextWithSize:newSize];
[UIImagePNGRepresentation(newImage) writeToFile:path atomically:YES];

return newImage;
}




+(UIImage *)drawImage: (UIImage *)image InContextWithSize: (CGSize) newSize {
UIImage *newImage;
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}
2

There are 2 answers

0
AOY On BEST ANSWER

The issue appeared to deal with the standard Xcode PNG compression option. One just needs to set it to NO if he doesn't wan't this to happen. I guess that this was made for the sake of retina displays.

1
Dheeraj Singh On

I have just changed few lines of code to get scaleFactor

-(UIImage *) createImageFromImage: (UIImage *)image WithName:(NSString *)name Thumbnail: (BOOL)isThumbnail{
    CGSize oldSize = image.size;
    CGSize newSize;
    CGFloat scaleFactorForWidth = 0.0,scaleFactorForHeight= 0.0;
    if (isThumbnail) {
        name = [NSString stringWithFormat:@"t_%@", name];
        newSize = CGSizeMake(125, 92);
    }
    else{
        newSize = CGSizeMake(320, 240);
    }

    if (!CGSizeEqualToSize(newSize, oldSize)) {
        CGFloat widthFactor = newSize.width/oldSize.width;
        CGFloat heightFactor = newSize.height/oldSize.height;

        if (widthFactor != 1) {
            scaleFactorForWidth = widthFactor;
             newSize.width = scaleFactorForWidth*oldSize.width;
        }

        if (heightFactor != 1) {
            scaleFactorForHeight = heightFactor;
               newSize.height = scaleFactorForHeight*oldSize.height;
        }
    }

    NSLog(@"New Size : (%f,%f)",newSize.width,newSize.height);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:name];

    UIImage *newImage = nil;
    newImage = [self drawImage:image InContextWithSize:newSize];
    [UIImagePNGRepresentation(newImage) writeToFile:path atomically:YES];

    return newImage;
}