Memory warning level 1 due to UIImage - iphone

I am working on one component where I need to scale and rotate image. following is flow of the component
Select the image from photo library -> show that image in UIImageView -> do the scaling -> save this image in document.
This works fine with image having low resolution.
but once I select the image with high resolution I first get Memory Warning level 1. but I cann't release that image, as I need to proceed further with same image.
I come to know image is unpacked by ( width * height * 4 ) so if I select image of 1800 * 1200 memory consumed is 8.6 MB [ Also checked with instrument ] .
Can any one help me to come over this issue? This creates 2 queuestion
can we use images with high resolution ?
what about 2 UIImageview with two high resolution images?
Thanks,
Sagar

You could change your order of operations a bit.
Select the image from the photo library -> scale the image -> save the scaled image to the Documents directory -> show the scaled image in the UIImageView.
Scaling and saving the image takes time, so your user will have a bit of a wait before they see the image in your UIImageView. Displaying an activity indicator with a cancel option is a good idea.
I've used this technique with pretty large images and it worked on older devices without incurring memory warnings.

Related

complication image is displaying too small

I have a watchkit application with an image. when I place the correctly sized (52x52 pixels for modular small 38mm) image into the correct place in the asset catalog the image displayed is very small, but when I just use a huge version of the image it is scaled down to the size I would expect:
any ideas/tips of how should I prepare/use correctly sized images for complications if I don't want to just go with the huge version?
source code: https://github.com/bazik123/Wristlight
ComplicationController.swift -> func imageNamed()
I had a similar issue and it ended up that the resolutions of my complication images were too high. I had them at 300 and reduced them to 72 using Preview and then they looked perfect!

iPhone - Resizing an UIImage in less than a second on an iPhone 4

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.

CCSprite memory overflow

I need to create and show 10 images using cocos2d. Each will be placed above previous one and all will be visible simultaneously (all of them has transparent areas). Each image has resolution 2048x1536.
When I create 5 or less CCSprites the app run fine, but when I create 6 CCSprite - it crashes on device (iPad) with error "Data Formatters temporary unavailable".
I suppose it's lack of memory, but maybe someone knows any approach for for this situation
Thanks!
A transparent image of this size uses about 12mb ram (2048*1536*4 bytes).
You are lucky that you get 5 images of this size shown before your app crashes.. which usually happens at about 50mb ram usage.
Without more knowledge about what you want to do or need to display I can't give any advice what to do .. but you won't be able to show 10 images of this size.
edit: since you are using cocos/opengl you might be able to get more images shown by changing the image format to rgb4444. This will cut the memory need to the half but you also loose much quality on your images.

Can we use higher resolution images in UIImageView?

Is it possible to use higher resolution images as it is i.e 1800 *1200.
After checking with instrument come to know. image unpacking is done by [ width * height * 4 ] and in this case it takes 8 MB.
Problem comes when we have two UIImageViews and both are holding UIImages with 1800 * 1200 res.
it is clear application will crash due to low memory. is this default behavior of UIImageView / UIImage
Because of this following things comes in focus
You cann't use higher resolution images.
If you want to use higher resolution images , then need to scale it down.
Thanks,
Sagar
There is a sample called PhotoScroller in Apple's Reference Library. It provides a way to display big images in chunks (tiles). That should do what you want.
You can if cut the images up into tiles. Since it isn't possible to display the entire 1800*1200 image at once you can just display the bits that are actually visible and cache the rest to disk. This is similar to how Google maps works.

UIImagePNGRepresentation() and scale (iPhone 4 screen)

I've got some image generating code that uses UIGraphicsBeginImageContext(), UIGraphicsGetImageFromCurrentImageContext() and UIImagePNGRepresentation() to do some drawing, then save it to disk as a PNG for later use.
Does UIImagePNGRepresentation() take into account scale? As in, if I have an image that is 20 points wide, will the resultant PNG be 20 pixels or 40 pixels?
Also, when I display these images, I use [UIImage imageWithContentsOfFile:] and [image drawInRect:]. Is there a way to hint to those methods to use higher resolution drawing?
Per the iPhone Application Programming Guide you should use UIGraphicsBeginImageContextWithOptions with a scale of 2.0 to create a properly scaled context for the iPhone 4.
As for the second part of your question, I believe you should save the resulting png with the #2x suffix on the base name (e.g., myImage#2x.png). Then when you load it back in using UIImage, its size and scale will be correctly set. Otherwise your image will be loaded at scale 1.0 and be twice as large (in points) as you expect it to be. This section of the same document goes into a bit of detail regarding high-resolution images for devices with high-resolution displays.