IOS-Integrated Bundle resource file Package

Source: Internet
Author: User

1. Bundle file
    • Bundle files, simple to understand, is the resource file package. We organize many pictures, XIB and text files together and package them into a bundle file. facilitates referencing resources within packages in other projects.

    • The bundle file is static, that is, the resource files that we include into the package are not compiled as a resource bundle. It also means that the bundle package cannot contain executable files. It is only as a resource and is parsed into a specific 2 binary data.

Back to top 2, making Bundle files
  • 1. New Bundle Project

    • To create a project named Sourcesbundle (the last bundle file name to generate), note that the bundle defaults to the MacOS system, which needs to be found under MacOS = Framework & Library options in Xcode high.

  • 2. Modify Bundle configuration information

    • Because the Bundle default is MacOS system, all need to modify his information, modify the IOS system.

    • Set the Build Setting COMBINE_HIDPI_IMAGES to NO, otherwise the image in the Bundle is in TIFF format.

  • 3. Optional configuration

    • As a resource bundle, just need to compile just fine, no need to install the related configuration, set the Skip install to Yes. Also remove the value of the installation path installation Directory.

    • The PCH file and the strings file for the resource bundle can be deleted.

  • 4. Add Files

    • Drag the resource file or folder below the Sourcesbundle folder in the project.

  • 5. Compile and generate Bundle files

    • We separately choose Generic IOS Device and any one of the emulators compiled once, after compiling, we will see the project in the Products folder Sourcesbundle.bundle from red to black.

    • Then show in finder and look at the generated files. We see that it generates a. bundle resource file for both the real machine and the simulator.

    • Select the. bundle file right-click to display the package contents, and we can see the resource files that were previously dragged into the project.

Back to top 3, using Bundle files
  • Drag the generated real-machine (DEBUG-IPHONEOS) Bundle resource file to the project you want to use.

  • 1. Load the Xib resource file in Bundle

        Set file pathNSString *bundlepath = [[NSBundle Mainbundle] Pathforresource:@ "Sourcesbundle" OfType:@ "bundle"];NSBundle *resourcebundle = [nsbundle Bundlewithpath:bundlepath]; //loading nib file uinib *nib = [UINib Nibwithnibname:@ "Bundledemo" bundle:resourcebundle]; nsarray *viewobjs = [nib Instantiatewithowner:nil options:< Span class= "hljs-literal" >nil]; //get xib file uiview *view = viewobjs.lastobject; view.frame = Span class= "hljs-built_in" >cgrectmake (20, 50, self.view.bounds.size.width-40,  Self.view.bounds.size.width-40); [self.view Addsubview:view];          
      • Effect

  • 2. Load the image resource file in Bundle

    • Specify the form of an absolute path

          UIImage *image = [UIImage imageNamed:@"SourcesBundle.bundle/demo2.jpg"];
    • form of Stitching Path

          NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"SourcesBundle" ofType:@"bundle"];    NSString *imgPath= [bundlePath stringByAppendingPathComponent:@"demo4"]; UIImage *image = [UIImage imageWithContentsOfFile:imgPath];
    • The form of a macro definition

          #define MYBUNDLE_NAME   @"SourcesBundle.bundle"    #define MYBUNDLE_PATH   [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:MYBUNDLE_NAME]    #define MYBUNDLE        [NSBundle bundleWithPath:MYBUNDLE_PATH]    NSString *imgPath= [MYBUNDLE_PATH stringByAppendingPathComponent:@"demo4"]; UIImage *image = [UIImage imageWithContentsOfFile:imgPath];
    • Effect

IOS-Integrated Bundle resource file Package

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.