美文网首页
UIImage And NSTimer

UIImage And NSTimer

作者: CombatReadiness | 来源:发表于2016-01-13 00:12 被阅读25次

    UIImage

    UIImage是用来显示图像的对象。我们可以通过文件、接收到原始的数据或者Quartz图像对象来创建UIImage对象。

    • imageNamed:类方法,根据指定的文件名返回UIImage对象
    • imageWithData:类方法,根据指定的NSData对象创建UIImage对象
    • imageWithContentOfFile:通过文件加载指定路径下的内容获得UIImage对象
    • imageWithCGImage:通过Quartz 2D对象创建UIImage对象
    • imageWithCIImage:通过Core Image对象创建UIImage对象
    • size属性:图像的大小,得到一个CGSize结构体,其中包括了宽度(width)和高度(height)
    
    // 此种方式只能小的图片
    UIImage *image1 = [UIImage imageNamed:@"abc"];
    NSString * strPath = [[NSBundle mainBundle] 
    pathForResource:@"one" ofType:@"png"];
    // 该方式即使加载很大的图片也不会使程序崩溃
    UIImage *image2 = [UIImage imageWithContentsOfFile:strPath];
    // 通过制定URL得到的数据创建图片对象    
    UIImage *image3 =[UIImage imageWithData:
    [NSData dataWithContentsOfURL:
    [NSURL URLWithString:
    @"https://www.baidu.com/img/bg.png"]]];
        
    

    NSTimer

    //下面五个参数依次为:
    1.间隔时间;
    2.事件源;
    3.SEL回调方法;
    (selector指定的方法必须是带一个参数的方法,并且那个参数的类型是NSTimer *);
    4.此参数可以为nil什么也不干,也可以;
    5.当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。
    NSTimer *timer = [NSTimer
    scheduledTimerWithTimeInterval:0.05 
                            target:self 
                          selector:@selector(buttonClicked:) 
                          userInfo:nil 
                           repeats:YES];
    - (void)buttonClicked:(NSTimer *)sender {
    NSString *string = (NSString *)[timer userInfo];
    }
    //用了时钟一定要记得销毁,通知也是一样
    - (void) dealloc {
        if (timer) {
            [timer invalidate];
            //这里指针设置为nil就不会出现野指针了
            timer = nil;
        }
    }
    

    相关文章

      网友评论

          本文标题:UIImage And NSTimer

          本文链接:https://www.haomeiwen.com/subject/kkjckttx.html