iOS - UIImageView常见使用方法和多图动画播放

UIImageView

显示图片

    // 创建对象
    UIImageView *imageView = [[UIImageView alloc] init];
    // frame
    imageView.frame = self.view.bounds;
    // 设置背景
    imageView.backgroundColor = [UIColor greenColor];
    // 设置图片
    imageView.image = [UIImage imageNamed:@"1"];

    /*
     // 带有Scale图片可能被拉伸
     UIViewContentModeScaleToFill,
     // Aspect比例,缩放是带比例的
     UIViewContentModeScaleAspectFit,
     UIViewContentModeScaleAspectFill,
     // 不会被拉伸
     UIViewContentModeRedraw,
     UIViewContentModeCenter,
     UIViewContentModeTop,
     UIViewContentModeBottom,
     UIViewContentModeLeft,
     UIViewContentModeRight,
     UIViewContentModeTopLeft,
     UIViewContentModeTopRight,
     UIViewContentModeBottomLeft,
     UIViewContentModeBottomRight,*/
    // 设置图片内容模式
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    // 是否裁多余的
    imageView.clipsToBounds = YES;
    // 创建毛玻璃
    UIToolbar *toolbar = [[UIToolbar alloc] init];
    // 设置毛玻璃尺寸
    toolbar.frame = imageView.bounds;
    // 设置毛玻璃的样式
    toolbar.barStyle = UIBarStyleBlack;
    toolbar.alpha = 0.9;
    [imageView addSubview:toolbar];    
    // 加载到控制器view中
    [self.view addSubview:imageView];

frame

第一种和第二种
// 创建对象
//    UIImageView *imageView = [[UIImageView alloc] init];
//    imageView.image = [UIImage imageNamed:@"1"];
    // frame设置方式
    // 第一种
//    imageView.frame = CGRectMake(0, 0, 500, 833);
    // 第二种
    UIImage *image = [UIImage imageNamed:@"1"];
    imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    imageView.image = image;
第三种
// 第三种
    UIImage *image = [UIImage imageNamed:@"1"];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    imageView.image = image;
第四种
// 第四种
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
// 修改位置
    imageView.center = CGPointMake(10, 10);
// 加载到控制器view中
    [self.view addSubview:imageView];

取图片

// 加载图片
    // 方式一
//    self.imageView.image = [UIImage imageNamed:@"1"];

    // 方式二
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];
    self.imageView.image = [UIImage imageWithContentsOfFile:path];
    // 放入Assets.xcassets中的路径,取不到,不能使用imageWithContentsOfFile:
    // 只能使用imageNamed:
    // 放入项目中的图片两中都可以取到

多图片组成动画

1.加载资源

这里写图片描述
如果建立真实文件夹,在调用文件夹中的东西的时候需要将路径全部写出,如果是虚拟文件夹则不用

2.抽取到的加载图片的方法
- (NSArray *)loadAllImageWithimagePrefix:(NSString *)imagePrefix count:(int)count{
    NSMutableArray<UIImage *> *images = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        // 获取图片名称
        NSString *imageName = [NSString stringWithFormat:@"%@_%d", imagePrefix, i + 1];
        // 以名称创建UIImage
        UIImage *image = [UIImage imageNamed:imageName];
        // 装入数组
        [images addObject:image];
    }
    // 返回图片数组
    return images;
}
- (void)viewDidLoad {
    [super viewDidLoad];  
    self.useImage = [self loadAllImageWithimagePrefix:@"Prefix" count:250];
}
抽取对动画播放的方法
- (void)playAnimationWithImagesArray:(NSArray *)imagesArray repeatCount:(int)count duration:(float)duration{
    // 设置播放动画图片
    self.imageView.animationImages = imagesArray;
    // 设置播放次数 0就是不限次
    self.imageView.animationRepeatCount = count;
    // 播放时长
    self.imageView.animationDuration = duration;
    // 播放
    [self.imageView startAnimating];
}
// 延迟实行方法
[self performSelector:@selector(fun) withObject:nil afterDelay:self.imageView.animationDuration];

图片的两种加载方式

imageNamed:
  1. 指向它的指针被销毁,该资源也不会干掉
  2. 放到Assets.xcassets的图片,默认就有缓存
  3. 适用范围图片经常使用
ImageWithContentsOfFile:
  1. 指向它的指针被销毁,该资源会被干掉
  2. 放到项目中的图片就不带有缓存
  3. 适用范围不经常使用的图片,大批量的图片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值