I've googled a lot about this question, but couldn't find anything interesting for me.
Q: Is there any class or method to change image quality(not size or scale, but the quality keeping the same size and scale).
As I understand there is no native(default) classes or methods to do this, am I right?
Any help would be appreciated.
If you have an image as a UIImage, you can use the UIImageJPEGRepresentation function, specifying a compression quality,to create an NSData object. This data object can then be used to create a new UIImage.
See the Apple Docs
This functionality is already available in iOS SDK itself to set image quality.
In the UIImage class, there is one method UIImageJPEGRepresentation.
In this function pass your image and image compression quality. It will not change dimension of the image but it will change the dip of the image to maintain the quality. It will help to maintain the size in memory as well.
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
Parameters:
- image : The original image data.
- compressionQuality: The quality of the resulting JPEG image, expressed as a value from 0.0 to 1.0. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality).
Hope this is what you required.
Enjoy Coding :)
Related
Currently I am dealing with the problem that I have pdf file with lots of 1024x768 images in it and am trying to optimize the pdf's file size, but the only solution that I thought is good enough for now is compressing the images with jpeg compression. The problem is that I did not find any way to do that with iOS APIs. Am I missing something, is there a way?
I`m welcome to suggestions on how to optimize the pdf with other means (lowering the resolution of the images is not a good solution for me).
Edit: If someone knows another API to use for pdf generation on iOS - links would be much appreciated.
Thanks in advance.
Actually you can use a UIImageJPEGRepresentation. But there's another step, to then use a JPEG data provider to draw the image:
NSData *jpegData = UIImageJPEGRepresentation(sourceImage, 0.75);
CGDataProviderRef dp = CGDataProviderCreateWithCFData((__bridge CFDataRef)jpegData);
CGImageRef cgImage = CGImageCreateWithJPEGDataProvider(dp, NULL, true, kCGRenderingIntentDefault);
[[UIImage imageWithCGImage:cgImage] drawInRect:drawRect];
This worked well for me when drawing to a PDF context. 0.75 compression quality reduced the size of my image-laden PDF by about 75% and it still looked fine.
If anyone is interested I found a solution to my problem by using a free pdf library - libHaru. It gave me the needed functionality to add a JPEG compressed image to the generated pdf file.
If you'd like to compress images you can use UIImageJPEGRepresentation
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
you could jpeg convert/compress a PNG image with
NSData *someImageData = UIImageJPEGRepresentation(pngImage, 0.6); // quality 0.6 (60%) (from 0.0-1.0).
UIImage *newCompressedJPG = [UIImage imageWithData:someImageData];
But i don't think so this will reduce your PDF size. Because when you place the UIImage to your pdf the RAW image get's placed (as far as i know).
Update 1:
its compressibility varies from 0.0 to 1.0 (thanks to Leena)
I have a matlab code and it generates a .png image of 1024*768 resolution. The images are about 450KB in size and I need to know how to optimise and compress these images using matlab.
Can't I play with the quality as in JPEG ?
I read the imwrite manual and don`t seem to find a good way to do this.
Is there any way to achieve it in matlab ?
By design PNG files are lossless - there is no 'quality' to be adjusted (it's probably why a mod changed your question title).
You can reduce the number of colors in the image (the color depth) which will in turn reduce filesize (PNG-8 instead of PNG-24, for example), but the whole point of PNG is it produces lossless images, so there is simple no quality value a la JPEG.
Taken from the manual :
A parameter of input in case it is JPEG:
'Quality' - A number between 0 and 100; higher numbers mean higher quality (less image degradation due to compression), but the resulting file size is larger.
imwrite(x,'c:\1.jpg','Quality',10)
edit: Sorry, I answered this one while the title was JPEG and not PNG.
PNG doesn't support any quality settings - it is a lossless format. The compression it applies is generally as good as possible.
Does scaling the image using: image=[UIImage imageWithCGImage:[image CGImage] scale:2.0 orientation:UIImageOrientationUp]; reduces the image quality?
If you mean something like compression artefacts then no, cause those are added when it comes to saving to disc in a specific format like JPG.
There is also a yes, because you double the dimensions of the image which will interpolate the pixels that never have been there, this will always look blurry starting at a certain point.
I am using following code to resize the image.
Resize a UIImage Right Way
And I use interpolation quality as kCGInterpolationLow.
And then I use UIImageJPEGRepresentation(image,0.0) to get the NSData of that image.
Still its a little bit high in size around 100kb. when I send it over the network. Can I reduce it further. If I am to reduce it more what could I do ?
Thanks and Kind Regards,
you image compress and image data stroed NSData format. The function is
UIImageJPEGRepresentation(UIImage * _Nonnull image, CGFloat compressionQuality);
Example:
NSData *objImgData = UIImageJPEGRepresentation(objImg,1.0);
I want to reduce the number of bytes of an image captured by the device, since i believe the _imageScaledToSize does not reduce the number of bytes of the picture (or does it?) - i want to store a thumbnail of the image in a local dictionary object and can't afford to put full size images in the dictionary. Any idea?
If you wish to simply compress your UIImage, you can use
NSData *dataForPNGFile = UIImagePNGRepresentation(yourImage);
to generate an NSData version of your image encoded as a PNG (easily inserted into an NSDictionary or written to disk), or you can use
NSData *dataForPNGFile = UIImageJPEGRepresentation(yourImage, 0.9f);
to do the same, only in a JPEG format. The second parameter is the image quality of the JPEG. Both of these should produce images that are smaller, memory-wise, than your UIImage.
Resizing a UIImage to create a smaller thumbnail (pixels-wise) using published methods is a little trickier. _imageScaledToSize is from the private API, and I'd highly recommend you not use it. For a means that works within the documented methods, see this post.
I ran into this problem the other day and did quite a bit of research. I found an awesome solution complete with code here:
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
You need to draw the image into a graphics context at a smaller size. Then, release the original image.
When you say 'physical size', are you talking about a print? Because you can just change the printer page size.
Are you talking about the number of pixels used to capture the image? As in, if you have a pixel array of 3000x2000, and you only want 150x150, then you can crop the images. At the time of capture, if you have a scientific imager, then you can just set the area that will be captured. The camera driver would include instructions for that. If you want to capture 3000x2000 in 1500x1000, you can try to bin the image, if that's what you need.
Or, you can use resampling post-capture in order to make the image smaller. One such algorithm is bicubic resampling, also linear resampling-- there are many variations.
I'm thinking this last is what you're most interested in... in which case, check out this Wikipedia page on the algorithm. Or, you can go to FreeImage and get a library that will read in the image and can also resize images.
UIImageJPEGRepresentation does the trick but I find that using the ImageIO framework often gets significantly better compression results for the same quality setting. It may be slower, but depending on your use case this may not be an issue.
(Code adapted for NSData from this blog post by Zachary West).
#import <MobileCoreServices/MobileCoreServices.h>
#import <ImageIO/ImageIO.h>
...
+ (NSData*)JPEGDataFromImage:(UIImage*)image quality:(double)quality
{
CFMutableDataRef outputImageDataRef = CFDataCreateMutable(kCFAllocatorDefault, 0);
CGImageDestinationRef imageDestinationRef = CGImageDestinationCreateWithData(outputImageDataRef, kUTTypeJPEG, 1, NULL);
NSDictionary* properties = #{
(__bridge NSString*)kCGImageDestinationLossyCompressionQuality: #(quality)
};
CGImageDestinationSetProperties(imageDestinationRef, (__bridge CFDictionaryRef)properties);
CGImageDestinationAddImage(imageDestinationRef, image.CGImage, NULL);
CGImageDestinationFinalize(imageDestinationRef);
CFRelease(imageDestinationRef);
NSData* imageData = CFBridgingRelease(outputImageDataRef);
return imageData;
}