iOS tutorials teach you to clean up pictures of iOS projects without

Source: Internet
Author: User

iOS training, iOS Tutorial project after the needs of the changes, product iterations, will go through several revisions, some of the previously no longer used images can not be cleaned up in a timely manner, these useless pictures on the one hand to make the project picture resource structure more complex, on the other hand will lead to the volume of IPA package becomes larger.

therefore need to clean up the unused picture resources, the use of lint in the Android project can easily accomplish this task, on iOS did not find a better tool, with the help of the online example, wrote a python to clear the image resources of Xcode.

First introduce the use of image resources in iOS, iOS using Images assets Management pictures, the project's picture resources are generally placed here, the picture will be placed in the *.imageset directory, which corresponds to 1x,2x and 3x diagram, There is also a Contents.json description file.

Clear the image resource needs to remove this directory, including image files and JSON files.

To clear out the unused image resources, you need to know how the image resources are referenced, the unreferenced picture resources can be erased.

The method of invoking a picture in code is typically:

[UIImage imagenamed:@ "image"]

The method of invoking a picture in Xib is described in XML, and the image resource is the name of the image:

  

So you need to search for the files in your project, and if you find the "name" for it, it means that the picture is being used, otherwise the image will be erased.

Search for files

The way to search for files can use Grep,ack is a good tool, but there is a faster, better way to search the contents of the file: the Silver Searcher.

The Silver searcher is more convenient, faster and easier to use.

Install the Silver Searcher:

Brew Install The_silver_searcher

You can search by using the AG command:

AG "Image" './'

The meaning of this command is to search for all files that contain an image under that directory and its subdirectories.

Bash command

Some bash commands are required to perform some search, delete work, and in Python you can run the Bash command directly.

You can use the Bash command directly in Python:

Os.popen (' echo $PATH ')

The bash command can be executed by Os.popen (' echo $PATH '). Read () reads the result of the execution.

You can also execute bash commands directly, such as:

Os.system (' Rm-rf dir ')

The bash command that needs to be used is the AG "image"./' and rm-rf './', which is used to delete a folder.

Find ImageSet resources not in use

Path = ' iOS ' images = Glob.glob (' Ios/images.xcassets/*/*.imageset ') def find_un_used ():

Img_names = [Os.path.basename (pic) [: -9] for pic in images]

Unused_imgs = [] for i in range (0, len (images)):

Pic_name = Img_names[i]

Command = ' AG '%s '%s '% (pic_name, path)

result = Os.popen (command). Read () if result = = ':

Unused_imgs.append (Images[i]) print ' Remove%s '% (Images[i])

Os.system (' rm-rf%s '% (Images[i]))

Text_path = ' Unused.txt '

Tex = ' \ n '. Join (sorted (UNUSED_IMGS))

Os.system (' echo '%s ' >%s '% (Tex, Text_path)) print ' Unuse res:%d '% (len (unused_imgs)) print ' done! '

This code is from the current folder, find all the. imageset files, and use the file name minus the suffix name, the folder to search for the name of the file has not been used. If it is not used, it can be removed.

After performing this paragraph it was found that some of the picture resources should not be cleared, as some exceptions were not taken into account.

Ignores

Sometimes the dynamic assignment of some resource files is used in this way:

for (int i = 1; i <=; ++i) {NSString *imagename = [NSString stringwithformat:@ "image_%d", I]; UIImage *image = [UIImage imagenamed:imagename];

......

}

When looking for iamge_1 in the code above, it is found that the image is not used and the resource is erased, but the picture is dynamically assigned in the project and should not be erased.

Use Ignore to solve this problem, that is, a ignore list is prepared in advance, the content is defined by regular expressions, and the file resources that are matched are not cleared, such as:

ignores = {R ' image_\d+ '}

Then define a method to check the Ignore:

def is_ignore (str):

For ignore in ignores:if re.match (Ignore, str): Return True

Return False

If a file name is in the Ignore list, it is ignored for check.

Please set your own directory, execute the command:

Python un_used.py

iOS tutorials teach you to clean up pictures of iOS projects without

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.