UI第四节——UIImageView详解

- (void)viewDidLoad {
    
    // super调用是必须的
    [super viewDidLoad];
    
    UIImage *image = [UIImage imageNamed:@"q.jpg"];

    // 实例化UIImageView,并设置其位置
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 335, 200)];
    imageView.backgroundColor = [UIColor redColor];
    
    // 设置UIImageView的图片
    imageView.image = image;

//需要设置图片 UIImage

第一种:[imageView setImage:[UIImage imageNamed:@"1.jpeg"]];

//第二种:
NSString *filePath=[[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpeg"];
UIImage *images=[UIImage imageWithContentsOfFile:filePath];
//[imageView setImage:images]; 

//第三种:
NSData *data=[NSData dataWithContentsOfFile:filePath];
UIImage *image2=[UIImage imageWithData:data];
[imageView setImage:image2];

//    设置圆角

    imageView.layer.masksToBounds = YES;

    imageView.layer.cornerRadius = 10;

   

    //    设置边框颜色和大小

    imageView.layer.borderColor = [UIColor orangeColor].CGColor;

    imageView.layer.borderWidth = 2;

    
    /**这三个模式是会缩放的,其他模式都不会缩放*/
    // UIViewContentModeScaleToFill 缩放去填充整个View的大小
    // UIViewContentModeScaleAspectFill 保持Image的宽高比,去缩放填充整个View的大小
    // UIViewContentModeScaleAspectFit 保持Image的宽高比,去缩放以适应View的大小
    
    // UIView的内容显示的模式
    imageView.contentMode = UIViewContentModeTopRight;
    
    // 剪切掉超出View的大小的内容
    imageView.clipsToBounds = YES;
    
    // 把UIImageView添加到self.view上
    [self.view addSubview:imageView];
    

//播放一系列的图片,
    NSMutableArray *animationImages = [[NSMutableArray alloc] init];
    for (int i=1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"player%d", i]];
        [animationImages addObject:image];
    }
    
    UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 240, 335, 200)];
    
    // 动画的一组图片
    animationImageView.animationImages = animationImages;
    
    // 播放一组动画的执行时间,单位是秒
    animationImageView.animationDuration = 2.0f;
    
    // 动画的重复次数,0是无限重复,默认是0
    animationImageView.animationRepeatCount = 0;
    
    // 播放动画
    [animationImageView startAnimating];
    
    
    // N秒之后,执行代码
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [animationImageView stopAnimating];
        
    });
    
    [self.view addSubview:animationImageView];
}

 PS:  frame: 该view在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统)
        bounds:该view在本地坐标系统中的位置和大小。(参照点是,本地坐标系统,就相当于ViewB自己的坐标系统,以0,0点为起点)
        center:该view的中心点在父view坐标系统中的位置和大小。(参照电是,父亲的坐标系统)

记住哦!!!!

如果对你有帮助,请关注我哦!

原文地址:https://www.cnblogs.com/laolitou-ping/p/6236912.html