Can we use higher resolution images in UIImageView? - iphone

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.

Related

Should I resize images from camera / photo library before sending over network?

The images I am getting back from the photo library and camera are HUGE, and it takes a significant amount of time to send them over the network even in good conditions. I can't imagine a scenario in which I would need the full double-digit MB image, but maybe I'm missing something? Is it common practice to resize them before sending them over the network?
Yes it is common practice to alter an image's dimensions and compression so that it is appropriate for application.
As observed, by the default images from the camera are very large so that they can be displayed on large screens, printed to posters, zoomed and cropped etc without appearing unduly pixelated. So unless the app in question is a photo/image manipulating app, then resizing will almost certainly be beneficial in that it can improve networking, the app's memory foot-print and it's overall snappiness.
In terms of how to size and compress, while it is possible to store multiple versions of the image for each device that's being supported.
Practically, it seems that storing a single image at the dimensions required on the lowest resolution device but then only compressing it enough so that the higher resolution devices can scale up as needed without looking ugly seems to deliver reasonable results.
In terms of resizing, there are various posts on SO e.g. How do I resize the UIImage to reduce upload image size
I've also stuck gist for the UIImage extension I'm currently using to do this in my App over on GitHub here.
Have fun.
You can transform the image into data and then use this:
let imageData = image!.jpegData(compressionQuality: 0.5)!
It will make the quality less than usual with 50% to be able to save it quickly and smoothly

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.

Large Image Causes Crashing xCode

I have a 6000x3000 px image that is in a zoomable view in my xCode project. In the initial view, a button is pressed to access the view with the large, zoomable image. This all works fine, except for the time that is taken to "load" the image often times causes a crash in the app, especially when I am testing on older devices (it seems to work fine most of the time on my 4G itouch). Is there any way to "pre-render" this one large image, or anything else that I can do to prevent crashing?
Do the math: 6000 x 3000 x 3 (red green blue) = 54,000,000 bytes = 51.5MiB of raw data. The normal image handling has a lot of overhead and that simply takes too much memory.
According to this question the solution is to use a CATiledLayer. As far as I have understood it, you need to divide your large image into smaller parts and draw these smaller parts with the help of CATiledLayer.
Edit: Here's a quote from the UIView class reference:
Note: In iOS 2.x, the maximum size of a UIView object is 1024 x 1024 points. In iOS 3.0 and later, views are no longer restricted to this maximum size but are still limited by the amount of memory they consume. It is in your best interests to keep view sizes as small as possible. Regardless of which version of iOS is running, you should consider tiling any content that is significantly larger than the dimensions the screen.
Read: If it's larger, use a CATiledLayer to draw smaller parts.

Memory warning level 1 due to UIImage

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.