UI04-UIImageView图片组件

来源:互联网 发布:程序员之死 事件女方 编辑:程序博客网 时间:2024/05/17 02:32

创建一个工程,在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....
原创粉丝点击