<span id="Label3"></p><p><p>What is bundle?</p></p><p><p>A bundle is a folder that organizes the directory structure according to a certain Standard. Each iOS app has at least one main bundle, which contains the binary code of the app and any resources, slices, sounds, HTML files, etc. that you use. In other words, the master bundle contains all the resources that are compiled into binary code to be submitted to the app Store.</p></p><p><p></p></p><p><p>What is the difference between bundles and common folders?</p></p><p><p>The 1.cocoa Touch Framework provides an interface for easy access to bundles and their internal resources.</p></p><p><p>2. If the bundle is added to xcode, the content in the bundle is changed in the local directory, and the bundle in Xcode will be aware of it and synchronize the changes in the Content. If you add a normal folder to Xcode and delete the resources under that directory in your local directory, the deleted resources will turn red in Xcode and need to be processed manually again. summary, any resource changes in the bundle, Xcode can synchronize the past, but the normal folder is Not.</p></p><p><p></p></p><p><p>What is the point of using bundles?</p></p><p><p>When our app is internationalized, the bundle is on the Pitch. Without bundles, programmers are required to maintain resources used by different countries and regions, some of which are public, and some are localized and expensive to Maintain. With bundles, we can store resource files according to the Bundle's criteria, without having to write code to judge the local Language. The method is simple, create the corresponding "localization folder", for example, The requirements are different areas of "test.png" display different content, We can create two localization folders Zh.lproj and en.lproj, respectively, to put two of the same name but different content test.png into the corresponding folder.</p></p><p><p></p></p><p><p>How do I use bundles?</p></p><p><p></p></p><p><p>To read the image object in the Bundle:</p></p><p><p>Nsstring*alansugarfilepath = [[nsbundle mainbundle]pathforresource:@ "alansugar" oftype:@ "png"];</p></p><p><p>If ([alansugarfilepath length]>0) {</p></p><p><p>Uiimage*image=[uiimage imagewithcontentsoffile:alansugarfilepath];</p></p><p><p>If (image!=nil) {</p></p><p><p>NSLog (@ "successfully loaded the file as an image.");</p></p><p><p>}else{</p></p><p><p>NSLog (@ "Failed to load the file as an image.");</p></p><p><p>}</p></p><p><p>}else{</p></p><p><p>NSLog (@ "Could not ' find this file in the main bundle.");</p></p><p><p>}</p></p><p><p>Read the NSData object in the Bundle:</p></p><p><p></p></p><p><p>If ([alansugarfilepath length]>0) {</p></p><p><p>Nserror *readerror=nil;</p></p><p><p>NSData *dataforfile= [[nsdata alloc] Initwithcontentsoffile:alansugarfilepath</p></p><p><p>Options:nsmappedread</p></p><p><p>error:&readerror];</p></p><p><p>If (readerror==nil&&dataforfile!=nil) {</p></p><p><p>NSLog (@ "successfully loaded the data.");</p></p><p><p>}else if (readerror==nil&& Dataforfile==nil) {</p></p><p><p>NSLog (@ "No data could be loaded.");</p></p><p><p>}else{</p></p><p><p>NSLog (@ "an Error occured while loading Data. Error =%@ ", readerror);</p></p><p><p>}</p></p><p><p>} else{<br>NSLog (@ "Could not ' find this file in the main bundle.");</p></p><p><p>}</p></p><p><p></p></p><p><p>Read NSData objects in a sub bundle, such as Resources (mainbundle)->images (childbundle)->alansugar.png (file)</p></p><p><p></p></p><p><p>Nsstring*resourcesbundlepath = [[nsbundle mainbundle]pathforresource:@ "Resources" oftype:@ "bundle"];</p></p><p><p>If ([resourcesbundlepath length]>0) {</p></p><p><p>Nsbundle*resourcesbundle=[nsbundle bundlewithpath:resourcesbundlepath];</p></p><p><p>If (resourcesbundle!=nil) {</p></p><p><p>Nsstring*pathtoalansugarimage=[resourcesbundle pathforresource: @ "alansugar"</p></p><p><p>oftype:@ "png"</p></p><p><p>indirectory:@ "Images"];</p></p><p><p>If ([pathtoalansugarimage length]>0) {</p></p><p><p>Uiimage*image=[uiimage imagewithcontentsoffile:pathtoalansugarimage];</p></p><p><p>If (image!=nil) {<br>NSLog (@ "successfully loaded the image from the bundle.");</p></p><p><p>}else{</p></p><p><p>NSLog (@ "Failed to load the image.");</p></p><p><p>}</p></p><p><p>}else{<br>NSLog (@ "Failed to find the file inside the bundle.");</p></p><p><p>}</p></p><p><p>}else{</p></p><p><p>NSLog (@ "Failed to load the bundle.");</p></p><p><p>}</p></p><p><p>} else{<br>NSLog (@ "Could not find the bundle.");</p></p><p><p>}</p></p><p><p></p></p><p><p>Use the [pathsforresourcesoftype:indirectory:] method to locate all resources under the specified folder:</p></p><p><p></p></p><p><p>Nsstring*resourcesbundlepath= [[nsbundle mainbundle]pathforresource:@ "Resources" oftype:@ "bundle"];</p></p><p><p>If ([resourcesbundlepath length]>0) {</p></p><p><p>Nsbundle*resourcesbundle=[nsbundle bundlewithpath:resourcesbundlepath];</p></p><p><p>If (resourcesbundle!=nil) {</p></p><p><p>Nsarray*pngpaths=[resourcesbundle pathsforresourcesoftype:@ "png" indirectory:@ "images"];</p></p><p><p>[pngpaths enumerateobjectsusingblock:^ (idobj,nsuinteger idx,bool*stop) {</p></p><p><p>NSLog (@ "Path%lu =%@", (unsigned Long) idx+1,obj);}];</p></p><p><p>}else{<br>NSLog (@ "Failed to load the bundle.");</p></p><p><p>}</p></p><p><p>} else{</p></p><p><p>NSLog (@ "Could not find the bundle.");</p></p><p><p>}</p></p><p><p></p></p><p><p>The meaning of bundles in iOS</p></p></span>
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