UI04-UIImageView图片组件

创建一个工程,在AppDelegate.m中编写代码:

#import "AppDelegate.h"

#import "ViewController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    

    [selfsetWindow:[[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds]];

     [self.windowmakeKeyAndVisible];

     [self.windowsetRootViewController:[[ViewControlleralloc] init]];

    

    returnYES;

}


在ViewController.m中编写代码:

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    [self.viewsetBackgroundColor:[UIColorwhiteColor]];

    

    UIImageView *imageView = [[UIImageViewalloc] initWithFrame:[UIScreenmainScreen].bounds];

    [imageView setBackgroundColor:[UIColorredColor]];

    [self.viewaddSubview:imageView];

    

#if 0

    //这种方式会将图片添加到内存

    [imageView setImage:[UIImage imageNamed:@"Ball.png"]];

    

    

    从本地读取图片,节省内存资源

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Ball" ofType:@"png"];

    NSData *imageData = [NSData dataWithContentsOfFile:path];

    NSdata类型转换为UIImage类型

    UIImage *image = [UIImage imageWithData:imageData];

    [imageView setImage:image];

    

    设置视图填充模式

    系统默认方式,将图片拉伸到全屏

    imageView.contentMode = UIViewContentModeScaleToFill;

    保持比例拉伸

    imageView.contentMode = UIViewContentModeScaleAspectFit;

   居中显示

    imageView.contentMode = UIViewContentModeCenter;

#endif

    //将图片组动画播放

    NSMutableArray *imageArray = [NSMutableArrayarray];

    for (NSInteger i =0; i < 10; i++) {

        UIImage *image = [UIImageimageNamed:[NSStringstringWithFormat:@"%zd", i]];

        [imageArray addObject:image];

    }

    [imageView setAnimationImages:imageArray];

    //播放间隔时间,此处为1s

    [imageView setAnimationDuration:10];

    //播放次数,0为无限循环

    [imageView setAnimationRepeatCount:0];

    //开始播放

    [imageView startAnimating];

    

}


@end


PS:在Assets.xcassets资源文件中导入了10张图片,并且命名方式为0.1.2....
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值