iOS Development Static Library (iii)--image, interface Xib and other resource files are encapsulated in the. A static library

Source: Internet
Author: User
Tags uikit

Build Environment: Macbook Air + OS X 10.9.2 + XCode5.1 + iphone5s (iOS7.0.3)

First, the resource files are packaged into bundles

New project: Project----------The Framework & Library--Bundle

We have to use OS X because there are no templates to create bundles in the iOS framework

Fill in the project name

Save location

In fact, this need not be so troublesome, the essence of the bundle is a directory, we can completely create a folder, the resource file needs to be copied to the directory, the suffix is changed to a bundle, and then you can use the interface provided by NSBundle to access its internal resource files, of course, You can also access its internal resources by accessing the normal file directory.

Here is mainly to consider the existence of the Xib file, from the previous knowledge, Xib is a text file, compiled to be serialized into the binary nib file, using the nib file is deserialized, you can display the interface normally. The bundle itself is static, and its internal resource bundle does not participate in the compilation of the project, so the project must be created here to serialize the xib into a binary nib, otherwise, create the folder directly after renaming it.

Therefore, some auxiliary files (the part of the red rectangular frame) are not needed, and can be deleted directly.

One thing must be very careful:

Delete the above red rectangle in the file, can be completely deleted, because these files are created by ourselves

When deleting the file in the rectangle below, you must not "Move to Trash", only "Remove References" (delete reference), because the framework is part of the system framework, if deleted, the next time, will be error. I made this low-level mistake and never thought that the system files would be deleted by myself, so I didn't think about it so much that it took a long time to solve the problem.

Since we have deleted the plist file, we must remove the reference to it in build settings

Also, remove the reference to the PCH file

We chose the framework in OS X when we created the project, so here we need to modify the Base SDK for iOS

After modification

Setup is complete, now copy the resource file to the project.

It's exactly what we need. Two image files, one xib file

All ready, press the shortcut key "Command + B" compile, after the successful compilation, the products below the mytoolswithassetsa.bundle from red to black, indicating the successful compilation

Locate the directory where the generated bundle resides

See what's Inside

Open it

Exactly what we needed, Xib was also translated into nib.

Second, compile the static library

Create a static library

In general, the name of the bundle is the same as the corresponding static library or framework (it can also be different), so here the static library is also named "Mytoolswithassetsa", which requires and the previous bundle project is stored in separate directories, of course, if you do not want to be so troublesome, You can also start a different name for the bundle project and modify it when you use it.

Delete unwanted files

Add to our documentation

#import <UIKit/UIKit.h> @interface Viewcontroller1:uiviewcontroller@property (Strong, nonatomic) Uiimageview * ImageView; @end
viewcontroller1.m//mytoolswithassetsademo////Created by LZH on 14-8-15.//Copyright (c) 2014 LZH. All rights reserved.//#import "ViewController1.h" #import "BundleTools.h" #import <quartzcore/quartzcore.h>@ Interface ViewController1 () @end @implementation viewcontroller1@synthesize ImageView = _imageview;-(ID) init{Nsbun        Dle *mybundle = [Bundletools getbundle];    self = [Super Initwithnibname: @ "ViewController1" bundle:nil]; Get interface file from bundle self = [super Initwithnibname: [NSString stringwithutf8string:object_getclassname (self)] Bundle:mybu    Ndle]; if (self) {//Custom initialization} return to self;} -(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [Super Initwithnibname:nibnam    Eornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;}    -(void) viewdidload{[Super Viewdidload];        Do any additional setup after loading the view from its nib. _imAgeview = [[Uiimageview alloc] Initwithframe:cgrectmake (50, 100, 220, 220)];    _imageview.image = [UIImage imagenamed: @ "0001.jpg"];        Get the picture resource from the bundle _imageview.image = [UIImage imagewithcontentsoffile: [bundletools Getbundlepath: @ "0001.jpg]];    Add rounded corners to the picture _imageview.layer.cornerradius = 20;    _imageview.layer.maskstobounds = YES;    _imageview.layer.borderwidth = 3; _imageview.layer.bordercolor = [Uicolor Orangecolor].        Cgcolor;    [Self.view addsubview: _imageview]; }-(ibaction) Backbutton: (ID) Sender {[self dismissviewcontrolleranimated:yes completion:null];} @end
  bundletools.h//  mytoolswithassetsa////  Created by LZH on 14-8-15.//  Copyright (c) 2014 LZH. All rights reserved.//#import <Foundation/Foundation.h> #define BUNDLE_NAME @ "Mytoolswithassetsa" @interface bundletools:nsobject+ (NSString *) Getbundlepath: (NSString *) assetname;+ (NSBundle *) Getbundle; @end
  bundletools.m//  mytoolswithassetsa////  Created by LZH on 14-8-15.//  Copyright (c) 2014 LZH. All rights reserved.//#import "BundleTools.h" @implementation bundletools+ (NSBundle *) getbundle{        return [nsbundle Bundlewithpath: [[NSBundle Mainbundle] pathforresource:bundle_name ofType: @ "BUNDLE"];} + (NSString *) Getbundlepath: (NSString *) assetname{        nsbundle *mybundle = [Bundletools getbundle];        if (Mybundle && assetname) {                return [[Mybundle ResourcePath] stringbyappendingpathcomponent:assetname];< c12/>}        return nil;} @end

  

Two images are used in this project, one is specified directly in the Xib file, the other is manually added, and a picture resource is obtained from the bundle.

"Command + B" compiles, generating static libraries for simulators and static libraries for real machines, respectively

Merge

III. Creation of Test engineering

Create a single view template under iOS

Project naming

Import static libraries, header files, and bundle resource bundles

Add a button to the main interface Main.storyboard, whose response function code is as follows:

Button response Function-(ibaction) buttonpressed: (ID) Sender {        ViewController1 *view1 = [[ViewController1 alloc] init];    View1.modaltransitionstyle = uimodaltransitionstylefliphorizontal;   Interface Toggle Animation effect    [self presentviewcontroller:view1 animated:yes completion:null];}

  

Description

We practice the encapsulation of resources such as images, Xib, and test engineering using storyboard, which looks a bit confusing, Mainly from the beginning of the XCode5.0, the creation of the project interface has been set to the default storyboard, here also do not want to toss, meaning expressed clearly OK, occasionally to point mix is good drop.

"Command + R" compile run, run result

Done!

iOS Development Static Library (iii)--image, interface Xib and other resource files are encapsulated in the. A static library

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.