What are the ways to reduce bundle size on iOS? - ios

I have some images which are huge in size and my bundle size currently is 70 MB. I think Xcode already runs the assets through png crush.

Do not use any text images with useless effects, use UILabels instead.
Draw simple shapes and effects using CAShapeLayers instead of using
images.
Use JPEGs instead of PNGs where you don't need transparency.
(Actually file size depends on the image content here)
Use Save for Web option in PhotoShop or other tools to optimize PNG
images.
Use sprites combined together instead of separate images.
Make sure you delete all unused resources.
Do not localize common parts of the images, localize only the
different parts. (think of a background image with a small flag at
the bottom for each locale. use one single bg image and separate flag
images. localize flag images only, not the entire bg images with the
flags.)
Use the same splash images for iOS7 and previous iOS versions. (You
need to manually edit the JSON file in .xcassets)
Try using a CDN to download assets on the first launch.
In addition to images keep those in mind too:
Try replacing custom fonts with default system fonts if you don't
need them really.
Use MP3 audio files instead of WAV files. (or other
compressed formats)
Make sure you delete all unused 3rd party frameworks.

You can try converting the images to jpg (if they don't have any transparent regions).
Also try using http://imageoptim.com/

It seems very unlikely that you actually need huge images. After all, the screen is not huge. Thus, the most likely form of size reduction is to reduce the physical dimensions of the images to the size you are actually going to be displaying.
This saves bandwidth when the user downloads the image, reduces the size of the app on the user's hard disk (the device), and also saves memory at the time an image is loaded. It is a vast waste of RAM to load an image that is larger than the size at which it will be displayed; after all, remember, the memory involved rises exponentially with the square of the difference.

One option is to host the images on a CDN like OpenText and fetch them as part of app initialization, or whenever they are first needed. Obviously this is more coding, but projects like SDWebImage make it pretty easy:
https://github.com/rs/SDWebImage/tree/master/SDWebImage
It also gives you the flexibility to swap out those images later if you use caching headers.

Related

Memory issue in storing and retrieving Image from database

I am working on application which include native SQLite database in which I am storing and retrieving images and showing into My application.
Now my problem is Like I am storing lots of images in directory and its path storing into database. So when I retrieve that path from database and load image into application, Memory Increases upto 10-20 Mb per image.
I also tried to store image data into database but same issue, Memory increase 10-20 Mb per image.
Please what should I do for this memory issue ?
Help me with it
Images, when used in the app, may require considerably more memory than the size of the asset in persistent storage might otherwise suggest. Assets are frequently compressed (e.g. JPG or PNG), but when you use the image, they're uncompressed, often requiring 4 bytes per pixel (one byte for red, green, blue, and alpha, respectively). So, for example, a iPhone 7+ full-screen retina image can require 14mb when you use the image. So, the memory-efficient technique is to employ lazy loading, not creating the UIImage objects until you absolutely need them. And, as Jerry suggested, because the amount of memory is determined by the size of the image and not the imageview in which you use the image, if your images have dimensions greater than required by the UIImageView in which you use them (i.e. width and height of the imageview times the "scale" of the device), you may want to resize the image accordingly.
It may be the case that the images you are trying to display are much larger than they need to be on the screen. Try loading the images into memory, creating a version with the size you need, and then using that. Of course, you could implement some caching so you don't have to keep resizing the same images. But then when the original image goes out of scope, its memory will be released. If you always need your images at the same size, try resizing them before storing them in the database, but if you want to support multiple sizes (for different devices, maybe), then store the images at the largest size you need and resize for the others.

iOS app crashes because images use too much ram

I know this is a stupid problem, but this is my first real app that I have to make, I have no one to ask and I looked up this problem and found no other similar problems.
My app crashes on real devices with no exception. I saw in the simulator that uses too much RAM and after a while I got to the conclusion that the pictures I am using are to blame.
The app is structured in this way: it has 8 viewControllers for different things: for example, it starts with one which lets the user select the avatar with which he/she will play and here I have two pictures, next is a viewController which shows the stats for that avatar and here it is another picture and so on. The problem is that each picture uses 40MB of RAM to be displayed and things add up so the app uses more than 300MB of RAM when the user gets to the gameviewCOntroller where the game is. Because of this, on devices like iPAD 2 or iphone 4 it crashes, but not on iphone 5.
I tried to set the images both from "images.xcassets" and from a ".atlas" folder, but the result is exactly the same. The pictures have a dimension of no more than 1500x1999px, they are in png format.
Also, I saw that if the app were to start directly into the gaveViewController it would use 180MB so the other viewController remain in memory or something like that. Should I "clear" them or something similar?
//-------update-------
This is what I got from Instruments:
Memory is a big deal on mobile devices, there is not a clear answer to you question, but I can give you some advices:
If your images are plain colors or have symmetric axes use resizable images. You can just use one line of pixel multiplied by with or height to cover the entire screen using a small amount of memory
Image compression doens't have effects when the image is decompressed. So if you have a png that is 600kb and you are thinking that converting in a 300kb will lower memory usage is only true for "disk space" when an image is decompressed in memory the size is widthXheightXNumber_of_channelXbit_for_channel
resize images: if are loading a 2000px square image into memory and you show it inside an image view of 800 px square, resize before adding it.You will have just a peak while resizing, but later it will use less memory
If you need to use big images, use tiling techniques such as CATiledLayer
If you don't need an image anymore get rid of it. It's ok to have an array of path to images, but not an array of full uncompressed images
Avoid -imageNamed it caches images and even if Apple says that this cache is released under memory pressure, you don't have a lot of control on it and it could be too late to avoid a crash
Those are general advices, it's up to you if they fit your requirements.
You should definitely follow Andrea's advices.
Additionally you should consider setting the image size to exactly what your need is. You're saying that you've tried to set them from xcassets so you have full control over the images you're loading, which is great (compared to downloading an image that you cannot modify).
I highly suggest you read some documentation on using Asset catalog files. This will allow you to have high-resolution image for bigger screens that also have more memory, and smaller ones for older devices, which is what you want here.
Also, note that 1500x1999px is still a very big size for most mobile devices.
More links about screen-size:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html
http://www.paintcodeapp.com/news/iphone-6-screens-demystified

reducing my ios application size

I am developing my first app (iOS universal app), I want to reduce my app's size because it contains many images (png files) and sounds(mp3 files).
So my problem is:
How can I reduce the size of my app (images and sounds)?
Thanks!
Images:
Only include the basics in your app bundle (i.e. app icons, launch image, and possibly images for the first page)
Use Parse (or any other similar service) to download any additional images after the app is downloaded.
This approach will significantly decrease the size of your app but also let you pull down additional image files as needed.
Sounds:
What is the type of sounds files you're using? .caf files are incredibly large. Using .aifc files are just as good quality (to my untrained ear at least) and takes up significantly less space
Depends, compress png images to jpeg usually reduce app size, there are also image optimizers that compress pngs. If your images are part of the UI, tile them or stretch them really helps you in reduce app size and also memory usage. The image asset function in Xcode 5 helps in you in create resizable images.
For sounds the concept are pretty close to images, use compressed file audio as eckyzero said.
If your sounds and images aren't part of the UI but resources, you can make the app download them from the internet at first launch.

How to minimize app size with heavy images in iOS Development?

I am going to work on client application, in which he need to have whole app locally resources loading, and I have almost 70mb images files for iPad. I am going to start development soon but before that I need healthy suggestion and guidelines to reduce my app size with these images use locally. I don't want to make this heavy size like any 3d game? So I am looking for suggestion what should I do? Thanks in advance.
There are few ways:
zip all the image resources in the bundle. On first launch, extract that zip folder into documents directory and refer the image from there only. You will have to do it on first launch only.
create jpeg version of your images with some reduced quality. Apart from icon and splash, you can use jpeg version of images.
If possible, use 1 pixel width/height images for repetitive gradients.

Use icns file as an image in iOS

Is there a way to use an icns file as the image source for I guess a UIImage? Or pick out one of the representations and resize?
NSImage does not seem to be available in cocoa-touch.
In particular I'd like to use them for drawing in the imageView part of a UITableViewCell.
There is no built-in way to do this and I see little benefit in doing so. It's best to just use the sizes that you need and ship them as individual images in your app. If you want to draw the images in a UITableViewCell, performance is an important aspect and resizing images won't be fast.
If you really need images in many different sizes and really don't want the best performance by including images for each size, you could use a PDF to make use of vector graphics. There is a nice article about this technique: http://mattgemmell.com/2012/02/10/using-pdf-images-in-ios-apps/
Honestly, don't try to use .icns files in your iOS app. It's just a container for images of different sizes and resizing is not a good idea, anyway. Either include individual images for any size you need or use vector graphics using a PDF file.

Resources