The use of iOS dynamic libraries and static libraries

At the beginning of

To build a high power grid application, library applications are essential, and at the same time libraries that want to really access iOS development are what we need to master.

The use of iOS dynamic libraries and static libraries
_ there is not far away

Concepts related to libraries in iOS

  • Library: a piece of compiled binary code, plus a top file that can be used by other people. Common libraries in iOS include.A,.Dylib,.Framework, and so forth.
  • Library Classification: open source library and closed source library,
    (1) open source library: for example, we commonly used AFNetworking, source code, are developed to our developers, copy downloaded to the project can be used directly. In development, we often use cocoapods to import the open source library directly, without having to worry about the dependencies of the open source library. In fact, the essence of cocoapods is to build an open source library into a static library, and then enable the winner project to rely on this static library.
    (2) closed source library: can be divided into static libraries and dynamic libraries, such as we used in the development of Baidu maps and other three party framework, will use a lot of closed source library.
  • Deep understanding of static and dynamic libraries
    (1) static library: it is your.M files are compiled into binary files, with exposed outside of the.H file, the program compile time will be directly copy to the target program, compiled after the completion of the actual library file is no more in effect, the target program without external dependencies that can be run directly, so lead to disadvantage is that the target program volume.
    (2) dynamic library: it is also your.M files are compiled into binary files, with exposed outside the file using.H, but the difference is that at compile time dynamic libraries are not copy to the target program, the target program will only be storing references to the DLL, until the program is running when the dynamic library will be loaded in real. It doesn’t affect the size of the target program, but it can cause some performance damage. And at the moment, apple doesn’t allow homebrew libraries to go to AppStore
  • The benefits of using static libraries and dynamic libraries:
    (1) static libraries: modularity, which avoids a small amount of change, often results in a large number of duplicate compiled connections, and can be reused, but not shared.
    (2): dynamic library can be the final executable file size, multiple applications share a library file in memory, save resources, can also be without recompiling the connection of the executable program, update the dynamic library file to update the application purpose. Sadly, however, apple doesn’t allow homebrew libraries to be added to AppStore, but it’s easier to develop in-house applications.

Creating a static library and a dynamic library

Below we will first create static and dynamic libraries, then we will create static and dynamic libraries applied to our project, so that we better understand the static and dynamic libraries, also facilitate better construction of their own projects or frame


Note: I wrote a little Demo, Demo’s function is to change the color of UIImage, I will create a static library and dynamic library to realize different function modules (of course, you can create a static library, but in order to let everyone know more of two kinds of database creation steps)
(1): static library package the resource file (.Bundle file) to store pictures, pictures and package method return path provided outside the.H file
(2): UIimage dynamic library package change color and provide external.H files.

By the way, the Bundle file and the Bundle file are static, that is to say, the resource file we include in the package as a resource package is not involved in the project compilation. This means that the executable file cannot be included in the bundle package. It is only used as a resource to be parsed into a particular 2 – digit data.


  • (1) creating a static library,
    , the first step: open Xcode, pop up the selection box below, and then you select the tag, then name the static library and create it.
The use of iOS dynamic libraries and static libraries
(1)

After creating a successful as you can see, Products under the libStaticLibraryTest.a file is red, not square, command+B or run to compile just fine, static library is a compiler is copy to the target in the program. As shown below, the system has created a project named.H and.M files for us. You can use it or not, and I use
directly

The use of iOS dynamic libraries and static libraries
(2)

Step second: create a Bundle file, the command+N will pop up the selection box, and then choose a mark and name to create
! [Uploading. 1.06.21_068118.png. 2016-11-17 screen snapshot morning.

The use of iOS dynamic libraries and static libraries
(3)

After the Bundle file was successfully created, I put a picture
into it

The use of iOS dynamic libraries and static libraries
(4)

The third step: add the Bundle file to Copy File, targets -&gt, build phases -&gt, copy files, as shown in the following figure

The use of iOS dynamic libraries and static libraries
(5)

The fourth step: write the correlation method

(1) provide external method interfaces in StaticLibraryTest.h

#import < Foundation/Foundation.h> @interface; StaticLibraryTest: NSObject + (NSString *) getImagePathWithBundleName: (* NSString) bundleName ImageName: (NSString * imageName) Type: (NSString * type); @end

(2) implementation methods in StaticLibraryTest.m

#import "StaticLibraryTest.h" @implementation + StaticLibraryTest (NSString *) getImagePathWithBundleName: (* NSString) bundleName ImageName: (NSString * imageName) Type: (NSString * type) {NSString *bundlePath = [[NSBundle mainBundle] pathForResource:bundleName ofType:@ "bundle"]; NSString *path = [NSString stringWithFormat:@ "%@/%@.%@", bundlePath, imageName, type]; return path @end;}

The fifth step: the execution of the program, command+R, so that we will generate a static library, click libStaticLibraryTest.a, and then show in fender, we will see below we generated for static library simulator, we pay attention to command+R in the simulator is run, if you want to generate a static library for real, must be in the field under the run lipo -create -output, and then through the command in the terminal with two static library files, so that we can get a real machine and can be used in the simulator on the static library (here is not to say). When applied, we will mark the copy to our project and we can use it directly

The use of iOS dynamic libraries and static libraries
(6)
  • (2) creating dynamic libraries;

The first step: open Xcode, pop up the selection box below, then select the tag, and then name the static library and create it.

The use of iOS dynamic libraries and static libraries
(7)

After the creation of the
, as shown

The use of iOS dynamic libraries and static libraries
(8)

The second step: create a ChangeImageColor class, used to write change the color of the picture method

ChangeImageColor.h

#import < Foundation/Foundation.h> #import < UIKit/UIKit.h> @interface ChangeImageColor: NSObject + (UIImage *), changeImageColorWithColor: (UIColor *), color, Image: (UIImage *) image, @end

ChangeImageColor.m

#import "ChangeImageColor.h" @implementation + ChangeImageColor (UIImage *) changeImageColorWithColor: (* UIColor) color Image: (UIImage * image) {UIGraphicsBeginImageContextWithOptions (image.size, NO, image.scale); CGContextRef (context = UIGraphicsGetCurrentContext); CGContextTranslateCTM (context, 0, image.size.height); CGContextScaleCTM (context, 1, -1.0); CGContextSetBlendMode (context, kCGBlendModeNormal) CGRect; rect = CGRectMake (0, 0, image.size.width, image.size.height); CGContextClipToMask (context, rect, image.CGImage); [color setFill]; CGContextFillRect (context, rect); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext (UIGraphicsEndImageContext); (return); newImage;} @ End

The third step: setting up the open header file, which can be set by target – &gt, build, phases – &gt, headers, as follows

The use of iOS dynamic libraries and static libraries
(9)

can also load its own header file
in DynamicLibraryTest.h

The use of iOS dynamic libraries and static libraries
(10)

The fourth step: General
dynamic library after three steps above we just created a dynamic library files, static libraries and the like but that does not support dynamic library while the prototype and simulator, you can create a generic dynamic library through the following steps

  • Create Aggregate, and name creation.
The use of iOS dynamic libraries and static libraries
(11)
  • Set Target Dependencies, targets -> Common -> Build Phases -> Target Dependencies; add dynamic libraries to Target Dependencies
The use of iOS dynamic libraries and static libraries
(12)
  • Add Run Script
The use of iOS dynamic libraries and static libraries
(13)
  • Add scripts, copy and paste just fine
The target folders and # Sets the final framework product. # if the project name and the Framework name of the Target is not the same thing, want to customize the FMK_NAME, there is no need to change the FMK_NAME=${PROJECT_NAME} Install will be the # dir final output to the framework. The line create it # following in the root folder of the current project. INSTALL_DIR=${SRCROOT}/Products/${FMK_NAME}.framework Working dir will be # deleted after the framework creation. WRK_DIR=build DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.Framework SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework -configuration ${CONFIGURATION} Clean # # and Building both architectures. xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk IPhoneOS clean build x Codebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator clean build Cleaning the oldest. if # [-d] "${INSTALL_DIR}" then RM -rf "${INSTALL_DIR}" fi MKDIR -p "${INSTALL_DIR}" CP "${DEVICE_DIR}/" -R "INSTALL_DIR}/ Uses the # A." Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product. lipo "${DEVICE_DIR}/${FMK_NAME}" -create "${SIMULATOR_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/${FMK_NAME}" RM -r "${WRK_DIR}"

The script after the command+B execution procedures, in the show in finder to find the dynamic library, we generated the same figure, I can only generate a dynamic library simulator, static libraries and the same principle, through the terminal to synthesize real machine and common dynamic simulator

The use of iOS dynamic libraries and static libraries
(14)

Applying static libraries and dynamic libraries

Through the above steps, we have generated a static library and a dynamic library, and then we began to use, very simple, step by step, just fine

  • Step 1: create a project, say more here.
  • The second part: the static library and dynamic library copy into the project, add as shown
The use of iOS dynamic libraries and static libraries
(15)

But this time you need to pay attention, static library copy can be used directly in the file, but the dynamic library must add dependent path

  • The third step: after you add the dynamic library, you also set the copy of framework as the resource file to Bundle, Targets–&gt, Build, Phases–&gt, Copy, Bundle, Resources, as shown in the following figure,
The use of iOS dynamic libraries and static libraries
(16)

Well, now we’ll move the static library and the dynamic library copy into our project, and then we’ll be able to use it.

project

Next, I have the following code for the ViewController.m file in the project

#import "ViewController.h" / / into a static library file #import "StaticLibraryTest.h" / / introduction #import dynamic library < DynamicLibraryTest/DynamicLibraryTest.h> @interface (ViewController) @property (nonatomic, strong) UIImageView * imageOfTest (nonatomic, strong); @property UIButton *button; @end @implementation ViewController (void) viewDidLoad {[super viewDidLoad]; / / Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; [self createSubView];} - (void) createSubView [[UIImageView alloc] (self.imageOfTest = {initWithFrame:CGRectMake (self.view.frame.size.width - 50) / 2, 200, 50, 50]); / / method provides a static library NSString *i MagePath [StaticLibraryTest getImagePathWithBundleName:@ = "Resource" ImageName:@ "advantage" Type:@ "PNG"]; self.imageOfTest.image [UIImage = imageWithContentsOfFile:imagePath]; [self.view = addSubview:self.imageOfTest]; self.button UIButton alloc] initWithFrame:CGRectMake "(20, 450, self.view.frame.size.width - 40, self.button.backgroundColor = 50); [UIColor cyanColor]; [self.button setTitle:@ change color forState:UIControlStateNormal] [self.view addSubview: [self.button addTarget:self; self.button]; action:@selector (buttonAction:) forControlEvents:UIControlEventTouchUpInside];} - (void) buttonAction: (UIButton * button) {NSLog (@" color change "); UIImage *image = nil; / / dynamic library provided by For the implementation of if (button.selected = = NO) {image [ChangeImageColor = changeImageColorWithColor:[UIColor blueColor] Image:self.imageOfTest.image];} else {image = [ChangeImageColor changeImageColorWithColor:[UIColor blackColor] Image: self.imageOfTest.image];} self.imageOfTest.image = image; button.selected = button.selected;}!

summary

The blog wrote a little longer, perhaps not good reading effect, originally wanted to split write, but always feel that the improvement of technology is not big. I hope you will understand.