user1811427
user1811427

Reputation: 359

How to reduce the image resolution and memory size when saving or while capturing an image in iphone

In my contacts application i used an image view to display the contact persons image,

in this process while saving the data user can save the contact person image as well (in the form of string, image file name).

I copy the images in sand box (Documents directory) and save the file names of images

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{

            NSString *DirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

            [[NSUserDefaults standardUserDefaults] setValue:storedPicsDict.contactImage forKey:@"oldContactPic"];

            //To SET the NEw IMAGE images from directory path
            CFUUIDRef uuid = CFUUIDCreate(NULL);
            CFStringRef generatedUUIDString = CFUUIDCreateString(NULL, uuid);
            CFRelease(uuid);
            NSString* hashKey = [(NSString*)generatedUUIDString autorelease];
            self.ContactImageFilePath = [NSString stringWithFormat:@"%@/%@.png",DirectoryPath,hashKey];

            storedPicsDict.contactImage = self.ContactImageFilePath;
            [contactPicture setImage:image forState:UIControlStateNormal];

            isNewContactImage = true;
      }


      [picker dismissModalViewControllerAnimated:YES];
}

the respective save image will displayed in contact's information.

But w*hen i saved few more images more than 6/7 it causes memory warnings and the and app gets crashed/slow down.*

So i need to save images with LOW RESOUTION and LOW MEMORY SIZE,

How is it possilbe, thanks

Upvotes: 2

Views: 4895

Answers (2)

Mihir Das
Mihir Das

Reputation: 478

1>Save the image in jpeg format so that image size gets reduced

NSData *imgData = UIImageJPEGRepresentation(your_UIImage_to_save, 0.5);
[imgData writeToFile:path_of_doc_dir_with_image_name atomically:YES];

2>

If you want to reduce the quality and size of the image you can use this code

CGSize newSize=CGSizeMake(50,50); // I am giving resolution 50*50 , you can change your need
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Hope this will help you..

Upvotes: 11

TedCheng
TedCheng

Reputation: 655

how about using this

UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25     orientation:original.imageOrientation];

and save the smaller image to a tmp file path for uploading. Do the images need to be .png? Otherwise, you may also try UIImageJPEGRepresentation to lower the image's quality.

Upvotes: 3

Related Questions