I m taking images from photo library.I have large images of 4-5 mb but i want to compress those images.As i need to store those images in local memory of iphone.for using less memory or for getting less memory warning i need to compress those images.
I don't know how to compress images and videos.So i want to know hot to compress images?
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData* data = UIImageJPEGRepresentation(image,1.0);
NSLog(#"found an image");
NSString *path = [destinationPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.jpeg", name]];
[data writeToFile:path atomically:YES];
This is the code for saving my image. I dont want to store the whole image as its too big. So, I want to compress it to a much smaller size as I'll need to attach multiple images.
Thanks for the reply.
You can choose a lower quality for JPEG encoding
NSData* data = UIImageJPEGRepresentation(image, 0.8);
Something like 0.8 shouldn't be too noticeable, and should really improve file sizes.
On top of this, look into resizing the image before making the JPEG representation, using a method like this:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Source: The simplest way to resize an UIImage?
UIImageJPEGRepresentation(UIImage,Quality);
1.0 means maximum Quality and 0 means minimum quality.
SO change the quality parameter in below line to reduce file size of the image
NSData* data = UIImageJPEGRepresentation(image,1.0);
NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);
OR
NSData *image_Data=UIImageJPEGRepresentation(image_Name,compressionQuality);
return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compressionQuality is 0(most) & 1(least).
Related
Can anyone suggest methods to enhance the quality of an image, being resized to a larger size?
On resizing, the resultant image pixelates. I need this pixelation to reduce to maximum extent.
You can use the following code for compressing or increasing size of an image.
CGFloat compression = 222.0f;
CGFloat maxCompression = 202.1f;
int maxFileSize = 160*165; //fill your size need
NSData *imageDat = UIImageJPEGRepresentation(image.image, compression);
while ([imageDat length] > maxFileSize && compression > maxCompression)
{
compression -= 0.1222;
imageDat = UIImageJPEGRepresentation(decodedimage.image, compression);
}
NSLog(#"image compressed success");
[image setImage:[UIImage imageWithData:imageDat]];
You can use the following code for compressing or increasing size of an image.
image1 = [self imageWithImage:image1 convertToSize:CGSizeMake(100, 100)];
// For convert the image to fix size
- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, 320,460)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
There is an Article called : Resize a UIImage the right way , you should Read and Understand each and every aspects given there. I think it will give Answers of Every Questions arises in your mind. I think it's a beautiful Article with proper Graphical Representation (means Less Boring at Reading).
I am getting an UIImage from UIGraphicsGetImageFromCurrentImageContext, but they are very heavyweight memory wise.
UIGraphicsBeginImageContext([self bounds].size);
// CGContext procedures
_cacheImage = UIGraphicsGetImageFromCurrentImageContext();
the size of the image is almost as the size of the iPad screen, (a little smaller), and when I do something like this:
NSData *data = UIImagePNGRepresentation(_cacheImage)
NSLog(#"%i", data.lenght);
It gives me like 700,000 in lenght. I'm guessing its a .7MB file?
Anyway if there is some way to reduce the image size, please let me know.
If you wish to reduce quality, try using
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
Pass compressionQuality == 0.0 for maximum quality or compressionQuality == 1.0 for maximum compression.
I am writing a .jpg file to my app's Documents directory like this:
NSData *img = UIImageJPEGRepresentation(myUIImage, 1.0);
BOOL retValue = [img writeToFile:myFilePath atomically:YES];
Later, I load that image back into a UIImage using:
UIImage *myImage = [UIImage imageWithContentsOfFile:path];
I know it works because I can draw the image in a table cell and it is fine. Now if I try to use UIImageJPEGRepresentation(myImage, 1.0), the debugger prints out these lines:
<Error>: Not a JPEG file: starts with 0xff 0xd9
<Error>: Application transferred too few scanlines
And the function returns nil. Does anybody have an idea why this would happen? I haven't done anything to manipulate the UIImage data after it was loaded. I just provided the UIImage to an image view in a cell. I set the image view properties such that all the images in the cells line up and are the same size, but I don't think that should have anything to do with being able to convert the UIImage to NSData.
The images were not corrupt, I was able to display them correctly. The issue is possibly a bug in UIImage, or perhaps the documentation should be more clear about using imageWithContentsOfFile:.
I was able to eliminate the error message by changing
UIImage *myImage = [UIImage imageWithContentsOfFile:path];
to
NSData *img = [NSData dataWithContentsOfFile:path];
UIImage *photo = [UIImage imageWithData:img];
I have the follwoing code to save the image but it seems poor in quality...
is there any way to get the images in a fine quality?
Thanks for any help
UIGraphicsBeginImageContext(CGSizeMake(self.view.bounds.size.width,
self.view.bounds.size.height-50));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, -50);
[self.view.layer renderInContext:context];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext( );
NSData* imageData = [NSData dataWithData:UIImageJPEGRepresentation( viewImage,0 )];
First thing that comes to mind is a compression quality parameter in UIImageJPEGRepresentation function - you set it to the lowest quality (0.0), try to set it to maximum quality value:
NSData* imageData = [NSData dataWithData:UIImageJPEGRepresentation( viewImage,1.0 )];
One option as it is mentioned in Vladimir's answer. Another option is you can use the UIImagePNGRepresentation. It will use the PNG format.
I have a little sketch program that I've managed to throw together using Quartz2D, but I'm very new to the iOS platform and I'm having trouble figuring out how to allow the user to save their sketch. I have a CGContextRef that contains the sketch, how can I save it so that it can later be retrieved? Once it's saved, how can I retrieve it?
Thanks so much in advance for your help! I'm going to continue reading up on this now...
There are different types of CGContexts. You are most likely having a screen based context, but you would need either a bitmapContext or a pdfContext, to create a bitmap or a pdf file.
See the UIKit Functions Reference. Functions that should be of interest are:
UIGraphicsBeginImageContext()
UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageJPEGRepresentation()
UIImageWriteToSavedPhotosAlbum()
UIGraphicsBeginPDFContextToFile()
UIGraphicsEndPDFContext()
Here's how I did it...
Saving
CGImageRef imageRef = CGBitmapContextCreateImage(bitmapContext);
UIImage* image = [[UIImage alloc] initWithCGImage:imageRef];
NSData* imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];
Retrieving
UIImage* image = [UIImage imageWithContentsOfFile:filePath];
CGImageRef imageRef = [image CGImage];