Reputation: 4764
I allow users to take a profile pic and then I need to upload it to the server. The iPhone pics are large in file size...much larger than needed for a profile pic and they take up a lot of space on the phone and also take a long time to upload to the server. I'm finding that on occasion if there is a weak Internet connection, the server times out half way through the upload.
I have code in a category to decrease the height and width of the image size but it does not seem to decrease the actual file size.
Can anyone suggest a lite way to do this--ideally without using a heavy library. Also, I'd like to avoid converting to jpg which I know allows for lossy compression.
Thanks.
Edit:
The problem might also be with how I work with the thumbnail after creating it below so I a including that code as well.
Here is code I am using to decrease height and width but it does not appear to reduce size in bytes.
@implementation UIImage (Thumbnail)
-(UIImage *) createThumbnailToFillSize:(CGSize)size
{
CGSize mainImageSize = size;
UIImage *thumb;
CGFloat widthScaler = size.width / mainImageSize.width;
CGFloat heightScaler = size.height / mainImageSize.height;
CGSize repositionedMainImageSize = mainImageSize;
CGFloat scaleFactor;
// Determine if we should shrink based on width or hight
if(widthScaler > heightScaler)
{
// calculate based on width scaler
scaleFactor = widthScaler;
repositionedMainImageSize.height = ceil(size.height / scaleFactor);
}
else {
// calculate based on height scaler
scaleFactor = heightScaler;
repositionedMainImageSize.width = ceil(size.width / heightScaler);
}
UIGraphicsBeginImageContext(size);
CGFloat xInc = ((repositionedMainImageSize.width-mainImageSize.width) / 2.f) *scaleFactor;
CGFloat yInc = ((repositionedMainImageSize.height-mainImageSize.height) / 2.f) *scaleFactor;
[self drawInRect:CGRectMake(xInc, yInc, mainImageSize.width * scaleFactor, mainImageSize.height * scaleFactor)];
thumb = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return thumb;
}
//Display image in viewdidload by calling above method:
UIImage *chosenImage = [self loadImageNamed:picname];
CGFloat side = 150;
side *=[[UIScreen mainScreen] scale];
UIImage *thumbnail = [chosenImage createThumbnailToFillSize:CGSizeMake(side, side)];
self.imageView.image = thumbnail;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.didChange=YES;
self.imageWasPicked = YES;//this is a dupe but used for updateButton
self.changedPic=YES;
[self updateSaveButton];
self.imageView.image = chosenImage;
//Should I be putting code to make thumbnail here instead of in display?
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Upvotes: 0
Views: 493
Reputation: 1466
If you are using UIImagePickerController in order to let the user choose a picture, make sure that you set
theImagePickerController.allowsEditing = YES;
This should already decrease the size of final image by 10-15 times.
If the received image is still too big, then here you can find some good examples of image resizing.
Upvotes: 2