`
yidongkaifa
  • 浏览: 4059163 次
文章分类
社区版块
存档分类
最新评论

iOS学习之NSBundle介绍和使用

 
阅读更多

bundle是一个目录,其中包含了程序会使用到的资源.这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in).对应bundle,

cocoa提供了类NSBundle.

我们现在用bundle获取程序里的一张图片,并显示到View上。


新建一个Single View Application,并在加入viewDidLoad方法里加入如下代码:

//    通过使用下面的方法得到程序的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];



在项目上右键,add图片文件图片文件QQ20120616.png。

运行程序:

打印出来图片路径如下

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

我们可以看到,图片在iOSSandbox.app这个包里,

图片显示:


图片取出来并显示了。




分享到:
评论

相关推荐

    Apple NSBundle 使用说明文档

    Apple NSBundle 使用说明文档

    IOS UI学习教程之区分NSBundle和NSURL(读取文件、写入文件)

    主要为大家详细介绍了IOS UI学习教程之区分NSBundle和NSURL,如何读取、写入文件,感兴趣的小伙伴们可以参考一下

    iOS Server 加载动态库 Demo (含Makefile)

    iOS界面app只可加载静态库,但server程序可以加载动态库。例子中含一简单实例,含Makefile。

    NSBundle读取图片 plist文件和txt文件

    http://blog.csdn.net/duxinfeng2010/article/category/1155790 博客有详细介绍

    ios WKWebView 的使用和交互

    本demo是WKWebView的基本使用和交互 ,实现了原生调用js的方法、js调用原生的方法、通过拦截进行交互的方法;修改内容 加入沙盒 / /加载沙盒 不带参数 // NSArray * paths = NSSearchPathForDirectoriesInDomains...

    IOS设备类型获取PLIST

    NSString *iOSDeviceModelsPath = [[NSBundle mainBundle] pathForResource:@"iOSDeviceModelMapping" ofType:@"plist"]; NSDictionary *iOSDevices = [NSDictionary dictionaryWithContentsOfFile:...

    iphone NSBundle 详解

    对于许多朋友来说 NSBundle比较陌生 本文详细介绍了bundle的使用,相信你一看便知 有iphone开发 兴趣的朋友加我qq 1748283579 注明csdn 加上自己工作经验,少于半年iphone开发经验的勿扰。。

    iOS之加载Gif图片的方法

    但是iOS竟然没有现成的支持加载和播放Gif的类。 简单地汇总了一下,大概有以下几种方法: 一、加载本地Gif文件 1、使用UIWebView // 读取gif图片数据 UIWebView *webView = [[UIWebView alloc] initWithFrame:...

    IOS开发中的各种Category

    NSBundle NSData NSDate NSDictionary NSException NSFileManager NSObject NSSet NSString NSTimer NSURL UIKit UIBezierPath UIButton UIColor UIDevice UIImage UIImageView UILable UINavigationController ...

    iOS开发教程之APP内部切换语言的实现方法

    国际化都会走到NSBundle的- (NSString *)localizedStringForKey:(NSString *)key value:(nullable NSString *)value table:(nullable NSString *)tableName方法,使用自定义Bundle替换NSBundle,通过切换根控制器来...

    ios开发小技巧

    iOS开发之UIlabel多行文字自动换行 (自动折行) UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(10, 100, 300, 180)]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, ...

    (0107)iOS开发之UI实时调试InjectionIII的使用

    使用步骤: 1.设置InjectionIII 打开InjectionIII工具,选择Open Project,选择你的代码所在的路径,然后点击Select Project Directory保存。 2.设置AppDelegate.m 打开你的源码,在AppDelegate.m的...

    在iOS中给视频添加滤镜的方法示例

    在 iOS 中,对视频进行图像处理一般有两种方式: GPUImage 和 AVFoundation 。 一、GPUImage 在之前的文章中,我们对 GPUImage 已经有了一定的了解。之前一般使用它对摄像头采集的图像数据进行处理,然而,它对本地...

    ios-datePickerView.zip

    self.datePicker =[[[NSBundle mainBundle] loadNibNamed:@"DatePickerView" owner:self options:0] lastObject]; self.datePicker.frame =CGRectMake(0, self.view.frame.size.height-260, self.view.frame.size...

    ABE_iOS_FilterDemo

    ##使用CIFilter来完成IOS中滤镜效果 在IOS中可以使用系统自带的方法来达到路径效果:需要用到的类为:CIContext, CIImage, CIFilter ####使用CIFilter实现滤镜效果的方法步骤如下: ​1.创建图像上下文CIContext ...

    tom猫游戏源码 iOS

    ios开发的tom,全部素材和源代码,战士部分代码 //调用button监听方法 -(IBAction)tomAction:(UIButton *)button{ [self tomAnimationWithName:button.currentTitle count:button.tag]; } //让tom猫动起来,name是...

    ios开发记录

    //一般使用bounds不设置x和y只设置宽和高 //center是相对于其父视图而言的,是CGpoint类型 _vi.bounds=CGRectMake(0, 0, 200, 200); // vi.center=CGPointMake(160, 240); _vi.center=_window.center; //arc4...

    WebpiOS:iOS加载webp后缀的图片(利用google开源c代码)

    iOS加载webp后缀的图片(利用google开源c代码) #usegae 1,cocoapod管理 pod'WebpiOS' 2,手动导入 把webp文件下的文件导入到工程,同时工程依赖SDWebImage第三方框架 导入头文件#import“ UIImage + WebP.h” ...

    自定义开关按钮(ios)

    例如,用图片代替:UIImage *normal_left = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"left_normal" ofType:@"png"]]; UIImage *normal_right = [[UIImage alloc] ...

Global site tag (gtag.js) - Google Analytics