UIImageView和UIImage
有两种方式:
方式一:
UIImage *image = [ UIImage imageNamed : @"1.JPG" ];
通过图片的路径加载图片,通过应用程序包找出图片 NSBundle(应用程序包类);
[NSBundle mainBundle] 获取当前应用程序包对象
pathForResource: 资源名称 ofType: 资源类型
UIImage *image2 = [ UIImage imageWithContentsOfFile :filePath];
第二种方式:如果这个图片资源只被使用一次,使用第二种方式,此种方式不会吧图片对象添加到应用程序缓存中,缺点:比较耗时
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
配置属性
设置 frame
imageView. frame = [ UIScreen mainScreen ]. bounds ;
重新设置图片
imageView.image = image2;
添加到父视图
[ self . view addSubview :imageView];
[imageView release ];
//1. 准备一组图片,使用数组存放
NSMutableArray *imageArray = [ NSMutableArray arrayWithCapacity : 7 ];
//2. 使用 for 循环添加图片
for ( int i = 1 ; i < 8 ; i++) {
// 确定图片的名字
NSString *name =[ NSString stringWithFormat : @"huoju_%d.tiff" ,i];
// 初始化 image 对象
UIImage *image = [ UIImage imageNamed :name];
// 将图片添加到数组中
[imageArray addObject :image];
}
NSLog ( @"%@" ,imageArray);
UIImageView *fireImageView =[[ UIImageView alloc ] initWithFrame : CGRectMake ( 120 , 100 , 79 , 106 )];
fireImageView. backgroundColor = [ UIColor redColor ];
fireImageView. layer . cornerRadius = 30 ;
// 设置 UIimageview 播放动态图片需要的数组
fireImageView. animationImages = imageArray;
// 设置播放动态图片的时间间隔
fireImageView. animationDuration = 0.1 ;
// 设置重复次数
fireImageView. animationRepeatCount = 100 ;
// 启动动态图片 , 对动画的配置写在动画开始之前 VIP
[fireImageView startAnimating ];
// 添加到父视图
[ self . view addSubview :fireImageView];
[fireImageView release ];
// 取出 8.JPG
NSString *filePath2 = [[ NSBundle mainBundle ] pathForResource</