I am using following code to resize an image - it all works well and as expected...
Resize UIImage the right way
I use interpolation quality as kCGInterpolationLow and UIImageJPEGRepresentation(image,0.0) to get the NSData of that image.
The problem is that the image size is still a bit high in size at around 100kb.
My question is can I reduce it further?
The images originate from the iPhone Photo Album and are selected via a imagePickerController.
Many thanks,
Related
I have a image which is uploaded in the image cloud-cloudinary using the API.The response of the upload gives me the cloudinary uploaded url.
Example of one image is as given:
This image is of 120*67 which is uploaded.Now in my app,it looks like this.
The width of my image is as per the phone screen width and the height is fixed to 324.Now i want to resize this 120*67 image to the width and height of my image in the app without losing its clarity.I have made the content mode as scale to fill for the imageview.
Generally, scaling up (non vectorized) images without compromising their quality is not possible (without machine learning). Some software programs have complex algorithms to help with upscaling, but that's also to a limited extent.
You should upload a larger version of your image to Cloudinary, and request it downscaled to the desired resolution.
You should also check out responsive layout design.
If the image is vectorized (e.g - SVG), you need to make sure you're not requesting a rasterized version of it. I.E -
https://res.cloudinary.com/<cloud name>/image/upload/fl_sanitize/<image name>.svg
will keep the image vectorized.
I'm working on a comic viewer app that downloads the latest content from a server. It downloads a single file regardless of the screen scale. What I'd like to do is make this file work correctly on both screens.
What's the procedure for this and how should I size the photos to fit? The trouble I'm having is that the graphics are retina screen size, but the iPhone doesn't interpret them as such. That means they're displayed twice as large as they should be.
CGImageRef cgImage = [myImage cgImage];
UIImage *retinaImage = [UIImage imageWithCGImage:cgImage scale:2.0 orientation:UIImageOrientationUp];
You could also change your image's dpi value in an image editor to make UIImage recognize the scale automatically.
Generally though, you should use lower-resolution images on devices that don't have a retina display because otherwise you're wasting precious memory.
For normal screen, you can resize them programmatically, by adding a scale category to the UIImage class by example, there are many code samples on stackoverflow like :
UIImage resize (Scale proportion)
For retina, you need to set the scale of the UIImage to let the ios know for what screen size it is used (you can set this with the initWithCGImage:scale:orientation: method of UIImage).
I am trying to figure out how I can shrink and save a UIImage, read it back in later, and then convert back to the original size without losing quality. I have managed to do the resizing and saving, but the problem is that if I save it smaller, when I read it back in and expand it, the quality is very poor. Does anyone know how this can be done without losing image quality?
You can't downsize the image and then bring it back without losing quality. You can't make something out of nothing, once you lose the data you lose the data.
You will need to save two versions of the image, one large and one small. This is a very typical scenario when dealing with thumbnails.
Check out the following site which provides categories for resizing images as well as several other really cool stuff:
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
Although you cannot upsize a downsized image, you can display a downsized one while retaining a reference to the original image (which you can save):
UIImage *image = [UIImage imageNamed:#"myImage"];
[image drawInRect: destinationRect];
UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);
The destinationRect will be sized according to the dimensions of the downsized version.
I am loading a UIImage of size 2480 × 3508 into UIImageView using UIViewContentModeScaleAspectFit. I am getting memory warning while loading this image. And after i loaded a few images of this size in this UIImageView the application crashes. Is there any method to solve this.
Solution to this problem consists of either loading scaled version of the image (*1) or you have option to display it in full resolution with help of CATiledLayer(*2).
[UIImage imageWithCGImage:scale:orientation:];
CATiledLayer example
Addendum: If your image source is Photo Library, ALASSet provides already scaled image to full screen and full resolution image. You can use combination of both with CATiledLayer. Full screen image is added to layer beneath the CATiledLayer to serve as placeholder while you're waiting for tiles in CATiledLayer to load.
Well, according to the documentation :
You should avoid creating UIImage objects that are greater than 1024 x 1024 in size.
Reduce your image then load it in your ImageView, there are plenty of libraries to do this, here is one :
https://github.com/Nyx0uf/NYXImagesUtilities
You can use this for scaling
The method is described simply in the following link:
How to scale a UIImageView proportionally?
I have a 5MP image coming from the back camera. I want to downsize it to put it into an ImageView without having to wait too long (it take a logn time to the UIImageView to display a 5 MP picture). So I tried many methods to resize / resample the image to make it fit a just-screen resolution (retina one). But it take around 1 sec to downsize the image.
Would you know any optimised way to be able to resize this 5MP image to the retina 960 x 640 resolution as fast as possible, and at least at less that 0.7 sec on an iPhone 4 ?
This is my favorite blog post and code to resize images. I haven't benchmarked it but I know of no faster way.
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
You're displaying an image in an UIImageView. I understand you want a version that is equal to the size of the screen. The way I would do it: display the original image in the UIImageView directly, this shouldn't take any noticeable amount of time.
Then in a background thread resize the same image down to the size you want and update the UIImageView as soon as that is done.
This gives you something on screen immediately while you generate an optimized version at the same time.
I don't know if you are using the original image for something else, but if you really only want the screen sized image it might be an idea to use an AVCaptureSession with a preset of AVCaptureSessionPresetHigh (see AVCaptureSession sessionPreset). This setting results in a more reasonably sized image, which might better suit your needs.