Tom
Tom

Reputation: 346

Reduce file size of UIImage going wrong

I'm getting some wierd results trying to reduce the filesize of a UIImage.


Code

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSData *data = UIImageJPEGRepresentation(image, 0.5f);
        UIImage *img = [[UIImage alloc] initWithData:data];

The variable img is what gets sent to the database. However it's never compressed.

What makes the whole thing even weirder is that it worked for a little while. That time i first tried to "fix it", but gave up and undid the changes. That's when it worked, when it went back to like before.

Later I made another change on a completely different part of the app, and again, the compression stopped working.

Does anybody have a clue of what's going on?

Upvotes: 0

Views: 255

Answers (1)

Codo
Codo

Reputation: 78985

Your code gets a UIImage instance from the image picker controller (line 1), then compresses it as a JPEG file (line 2) and then expands (uncompresses) it again (line 3).

You don't want to send the expanded image (UIImage instance called img) to the database. Instead, you'll want to send the compressed JPEG image (NSData instance called data) to the database.

BTW: Did this code ever work? Is it possible at all to store a UIImage instance?

Upvotes: 1

Related Questions