创建一个工程,在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;
}
#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