APP compression solution 3 + 1, easily deal with 20 m limit

Source: Internet
Author: User
Tags ziparchive

From: http://www.aiw3.com/iphone/0504969.html

 

APP compression solution 3 + 1, easily deal with 20 m limit

 

Solution 1:
With the efficient PNG image compression software, you can first slim down all images.

We recommend imageoptim, an image compression software for Mac. For a convenient trial, as long as you simply drag the file or folder to be compressed into the software, it will be automatically compressed.
Note: 1. You don't need to select it because it will automatically recognize the file format, and it won't process it in Non-image format, so don't worry, even if you drag it !~
2. It will overwrite the original file during compression, so you are worried that the original file is damaged. Please pay attention to the backup. So far, no problems have been found.
3. After compression, the image may fail to be previewed. You don't have to worry about it. You can compile the image as usual!
4. The compression rate depends on the specific image file. Generally, the average compression rate is more than 50%, and the highest is 94%. Ha ~
5. Last, we would like to remind you that you can compress a series of images multiple times to find the maximum compression ratio. "X" will be displayed when the compression is no longer needed, so please feel free to use it!

Solution 2:
Noise reduction for music sound effects, and pay attention to the music format!

I am not good at music, so it is inconvenient to comment more. Now there are a lot of music processing software on the market, and the functions are exceptionally powerful. The suggested solution is to use all methods to make him smaller,
As long as you don't feel uncomfortable!

Note: we have more than 20 mb of music files in our project. After a series of processing, only 800 K !! More importantly, the average person does not hear the difference at all!
Therefore, it is necessary to compress music !!


Solution 3:
If your requirements are not met after the above two points are processed. (For example, the size after processing is about 21 MB, which is almost the same !) The last step is also technical compression.
Use zip compression technology to compress images and music in resource files.


First download a zip open-source class, a lot of online. I use -- zipfile-OC. It is also called minizip. This open-source class is specifically written for obj-C. Encapsulation is better, and its usage is simple.
(Http://linglong117.blog.163.com/blog/static/277145472009101814159283/) Go To The cloudmonitor log. This log introduces two zip methods. I use the second one.
Download zipfile-OC is included at the end of the article. The specifics are no longer repeated. See the original document .~ Here, I would like to thank yunshuixinxin.

The following describes how to compress resources.

First, let's take a look at how xcode processes files in resources. As long as you find the compiled app file in the project and right-click to display the package content, you will find that, during compilation, xcode copies the files in resources from their respective folders to the app for calling.

What we need to do is to extract and compress these files into ZIP files in advance. Delete the original resource and put the compressed zip file into resources for compilation. Note: folders are not allowed. All images and music must be compressed in the same directory.

Here we will encounter a problem, that is, how to decompress these images when the program reads them?
Note that Apple does not run developers to modify app files in programs. Therefore, you must find a modifiable folder to store the extracted files. That is the document folder (each application has a folder ).

You only need to decompress the required resource file to the document directory before the program reads the resource (this location depends on how your program code is written. I don't need to talk about it, then modify the address for reading the resource.

 

To facilitate understanding by new users. Add the code. Take a cocos2d framework game as an example.
Add the zip code at the beginning of the following function.

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
{
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0]; // The first two sentences are the addresses used to obtain documents from the real machine.
Nsstring * ImagePath = [documentsdirectorystringbyappendingpathcomponent: @ "/A Resource Name"];
// Obtain the address after extracting a resource. Note that "/" is indispensable. The file address is used as a basis for determining whether the resources have been compressed to avoid repeated decompression.

Nsdata * Data = [nsdata datawithcontentsoffile: ImagePath];

If (Data = NULL) // if the flag file does not exist, the first time the program runs on the surface, extract the required resource file and skip it if it exists.
{

Nsstring * loadimagepath = [[documentsdirectorysubstringtoindex: documentsdirectory. length-9] stringbyappendingpathcomponent: @ "project name (Product Name)/compressed file name (*. zip) "]; // obtain the compressed file in the Resource

Ziparchive * Za = [[ziparchive alloc] init];

If ([za unzipopenfile: loadimagepath]) // Extract
{
Bool ret = [za unzipfileto: documentsdirectory overwrite: Yes];
If (NO = RET)
{
}
[Za unzipclosefile];
}

[Za release];

}
/* Omit other code */
Return yes;
}

You don't have to worry that the decompression process will take a long time. It takes less than 3 seconds to roughly estimate the size of the 20 m compressed file. In addition, you only need to wait for the first time the program runs, and then it will not be decompressed.

The rest is to modify the original resource path. I don't know if I can directly modify it in xcode.

We adopt the most direct and perhaps the most stupid method, that is, directly modifying the underlying category of image and music processing. Take cocos2d as an example.

Modify addimage (-(texture2d *) in the texturemgr. M class *)
Addimage: (nsstring *) path;) method. Add the following three sentences to the beginning.


Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [pathsobjectatindex: 0];

Path = [[documentsdirectorystringbyappendingpathcomponent: @ "/"] stringbyappendingpathcomponent: path];
The purpose of these three statements is to add the paths of the Documents folder before a file name.

Note: 1. Do not place systems and Framework resources such as icon.png/*. plist/defalut.png into a compressed package.
2. If the first two solutions are used, the efficiency of this solution is limited.

Finally, let's talk about a solution.
That is, the zip compression provided by Apple. This may be known to many people who have published a project. before publishing a project, they need to compress the app file into a zip file before uploading it. This step requires at least 6 MB of space.

Note: during actual operations, it is found that if a project is compressed to 20 mb after being zip, it will be about MB after being released. I don't know if it is caused by Apple's Mbit/s?

* The following is a reference to the 6l solution ~
The extra m is mainly used by Apple for signature verification and account fee binding. That is to say, the same app downloaded by different accounts using iTunes is different.
Thank you ~~ Learning!

Finally, attach the following data to the actual test:
The size of an unprocessed app is about 50 m +. Image resources account for 18 MB. Music resources accounted for 27 MB. About 6 MB is occupied by openfeint (about 12 Mb uploaded online, about 3 MB of imported resources, and the remaining 3 MB should be in the compilation file ).
After processing the first two solutions, there will be less than 10 MB of images, and only 1 MB of music. (The sound quality may be poor under professional equipment, but I believe that, generally cannot hear it ). Openfeint is not processed, but it is still 6 MB. The compiled app is 22.4m. After Zip compression, it is added with Apple's signature verification (the fourth solution ). The final size is only 16.5 m.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.