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.