Search code examples
xcodewidthnsdatauiimagejpegrepresentation

Xcode NSData Image Size Change


I want to save an image. Therefore I convert it to a NSData. However during these steps the width and height of the image will change. It is increased in size. This happens on the iPad Air series, before the width/height remained the same.

NSLog(@"BEFORE %f %f",image1.size.width,image1.size.height); // Displays 200 133

// Convert to NSData
NSData *imagedata = UIImageJPEGRepresentation(image1,0.8f);

// Store etc.

// Convert it back
UIImage *image2=[UIImage imageWithData:imagedata];

NSLog(@"AFTER %f %f",image2.size.width,image2.size.height); // Displays 400 266

I do not want the imagesize to increase!


Solution

  • For those of you who still want the answer to this question, as Vincent pointed out, there is a scale factor to images. Use the following code to fix it:

    UIImage *originalImage = [UIImage imageNamed:@"image.png"];
    UIImage *scaledImage = [UIImage imageWithCGImage:[originalImage CGImage]  scale:1.0 orientation:(originalImage.imageOrientation)];