您的位置:首页 > 产品设计 > UI/UE

UIImageView的属性与方法

2015-09-04 13:23 453 查看
  bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle.我们的程序是一个bundle. 在Finder中,一个应用程序看上去和其他文件没有什么区别. 但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录.
我们把这个目录叫做程序的main bundle。

获取图片

  1.   NSString *path = [[NSBuddle mainBuddle] pathForResource:@"resourceName" oftype@"resourceType"];

       UIImage *image = [UIImage imageWithContentsOfFile:path]

];   //使用缓存,一般用小图片

  2.   UIImage *image = [UIImage imageNamed:@"imageName"];

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow
alloc]initWithFrame:[[UIScreen
mainScreen]bounds]];
    iv = [[UIImageView
alloc]initWithFrame:CGRectMake(30,
100, 62,
74)];
    //uiimage 直接使用类的方法UIImage imageNamed用到了缓存
  
    // UIImage * ig = [UIImage imageNamed:@"nc.png"];
    self.window.backgroundColor=[UIColor
whiteColor];
    //得到图片nc.png的路径
    NSString *path = [[NSBundle
mainBundle]pathForResource:@"nc"
ofType:@"png"];
   
    //UIImage imageWithContentsOfFile
从路径中得到图片传给ig
    ig = [UIImage
imageWithContentsOfFile:path];
    NSLog(@"%@",path);
    //把image挂在imageview上
    [iv
setImage:ig];
    
    UIButton * but = [[UIButton
alloc]initWithFrame:CGRectMake(130,
110, 50,
40)];
    
    [but setTitle:@"转换"
forState:UIControlStateNormal];
    
    but.backgroundColor=[UIColor
blueColor];
    [but addTarget:self
action:@selector(ac)
forControlEvents:UIControlEventTouchUpInside];
    [self.window
addSubview:but];
    [self.window
addSubview:iv];
    [self.window
makeKeyAndVisible];
    return
YES;
}

-(void)ac
{
    if (flag==NO) {
        ig=[UIImage
imageNamed:@"nc.png"];
        [iv
setImage:ig];
        flag=YES;
    }
    else
    {
        ig=[UIImage
imageNamed:@"zd.png"];
        [iv
setImage:ig]; 
        flag=NO;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: