how to save Image to Photos in iPhone without losing quality? - iphone

the quality of saved image will lose when i'm using this method ...
UIImage *img=imageview1.image;
UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);

You need to wrap the image in a PNG representation, so that it's saved to the Photo Library in PNG format, rather than JPG format.
I used the following code based on the code from Ben Weiss: UIImageWriteToSavedPhotosAlbum saves to wrong size and quality for a side by side comparison.
// Image contains a non-compressed image stored within my Documents directory
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
NSData* imdata = UIImagePNGRepresentation ( image );
UIImage* im2 = [UIImage imageWithData:imdata];
UIImageWriteToSavedPhotosAlbum(im2, nil, nil, nil);
The image will be saved in the PNG format in the Photo Library so that it can be accessed/shared later in full quality.

The function
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
enables you to get the data representation of your image for the expected compression quality (1 is best).
Then you just have to write your NSData to the file system to get the JPEG file (using writeToURL:atomically: on NSData for instance).
The same can be applied to .PNG with the UIImagePNGRepresentation.
The original doc :
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImageJPEGRepresentation

I don't know how to do it in the SDK, but though the UI, if you copy + paste the image into a message, it keeps its full size. Perhaps there's some mechanism like that in the SDK (copy to clipboard)? Dunno. Hope it gives you an idea.

When using:
UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);
The compression quality is about 0.75
image?.jpegData(compressionQuality: 0.75)
Try it yourself for proof. 🙂

Related

Size of UIImage prepared from downloaded NSData shows 0,0 even though data length is >0

On server an image is uploaded which is either jpeg, bmp or png.
When the image is downloaded then NSData shows its length to be > 0 like 173803 etc.
However, when I make UIImage out of this data via the following method and check its size then it comes 0,0.
UIImage *receivedImage = [UIImage imageWithData:receivedNSData];
NSLog(#"Image.size = %#",[NSValue valueWithCGSize:receivedImage.size].description]);
//output of log
// Image.size = NSSize{0,0}
ImageView also doesn't display the image, when set on it. Other images locally converted to NSData and then back to UIImage will display in the imageView.
Can anyone suggest me a solution to this ?
Edit 1 : Yes, I am receiving the data properly. I am getting data.length to be greater than zero like 1789938, etc. as I mentioned above initially.

Binary row data to image conversion

How can I convert binary data to image ?
I have binary file which content of RGB 565 bitmap image. i want converted .bin file in to image.
Thank's in advance.
UIImage *retrieved = [UIImage imageWithData:
[NSData dataWithBytes:data length:length]];
After you get the data, you have to first convert it to a native format of iOS (RGBA8888, ARGB8888 or RGB888). This method should help you with that. Now all you have to do is get the data inside of a vBuffer. After you get the data out as RGBA8888, you need to make a CGBitmapContext with the data and get a CGImage out of that. So, in the end, I suggest you switch formats if you can. You won't save any performance by doing it this way.

How decrease the size of image jpeg/png

I create an image using the following method by passing the two parameter one is UIImage object is imagdata and NSString object is storeImage
JPG: [UIImageJPEGRepresentation(imageData, 1.0) writeToFile:storeImage atomically:YES];
or
PNG: [UIImagePNGRepresentation(imageData) writeToFile:storeImage atomically:YES];
my problem is that, original image size is 2.1 MB after i using the above method the image size is 4.2 MB in simulator.
I don't want to use any compression method and i don't want to loss any quality of image. i want copy the image as it is in given path. in actual size.
Use simple data object of your image.Try following code where imageData in NSData object.
[imageData writeToFile:storeImage atomically:YES];
could you tell me which is the original size and format? One way to reduce the size is lose a little of quality.
[UIImageJPEGRepresentation(imageData, 0.5) writeToFile:storeImage atomically:YES]
Another way could be use ImageIO framework.

Can I use JPEG compression for images in pdf files generated on iOS?

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)

Reduce UIImage size to a manageable size (reduce bytes)

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;
}