Introduction and use of NSBundle for iOS Learning

Source: Internet
Author: User

Bundle is a directory that contains the resources used by the program. these resources contain images, sounds, compiled code, and nib files (bundle is also called plug-in ). corresponding bundle,

Cocoa provides the NSBundle class.

Now we use bundle to get an image in the program and display it on The View.

 

Create a Single View Application and add the following code to the viewDidLoad method:

 

// Obtain the main bundle NSBundle * mainBundle = [NSBundle mainBundle]; NSString * imagePath = [mainBundle pathForResource: @ "QQ20120616-1" ofType: @ "png"]; NSLog (@ "% @", imagePath); UIImage * image = [[UIImage alloc] initWithContentsOfFile: imagePath]; UIImageView * imageView = [[UIImageView alloc] initWithImage: image]; [self. view addSubview: imageView];

 

Click "add image file" on the right of the project to add qq20120616.png.

Run the program:

The printed image path is as follows:

 

/Users/rongfzh/Library/Application Support/iPhone Simulator/5.1/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/iOSSandbox. app/QQ20120616-1.png

We can see that the picture is in the iOSSandbox. app package,

Image Display:


The image is retrieved and displayed.

 

 

Related Article

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.