标签:style ar io color os 使用 sp for strong
UIImageView是显示图片的控件,显示图片时,首先需要把图片加载到UIImage控件中,然后根据UIImage控件,设置到UIImageView相对应的属性中。
//使用应用程序中图片文件创建,IOS4后可以省略图片扩展名; + (UIImage *)imageNamed:(NSString *)name; //根据指定的路径创建UIImage + (UIImage *)imageWithContentsOfFile:(NSString *)path; - (instancetype)initWithContentsOfFile:(NSString *)path; //使用NSData创建 + (UIImage *)imageWithData:(NSData *)data; - (instancetype)initWithData:(NSData *)data; //使用Quartz 2D对象创建UIImage + (UIImage *)imageWithCGImage:(CGImageRef)cgImage; - (instancetype)initWithCGImage:(CGImageRef)cgImage;
注意:
UIImage imageNamed 会自动缓存图标,会占用大量内存,推荐使用 IImage imageWithContentOfFile:(NSString *);
//单个图片对象 @property(nonatomic,retain) UIImage *image; //用数组存储的多个图片对象 @property(nonatomic,copy) NSArray *animationImages;
//创建image对象 UIImage *image = [UIImage imageNamed:@"imageName"]; UIImage *image = [UIImage imageWithContentsOfFile:[bundle pathForResource:@"imageName" ofType:nil]] //添加image对象到UIImageView中 UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
查看UIImageView头文件的相关属性
//设置图片,animationImages 是NSArray数组中包含的UIImage @property (nonatomic,copy) NSArray *animationImages; //设置播放次数,不指定为无限循环播放 @property (nonatomic)NSInteger animationRepeatCount; //设置播放速度,动画持续的时间 @property (nonatomic)animationDuration; //启动动画 - (void)startAnimating; //停止动画 - (void)stopAnimating; //判断动画是否在运行 - (BOOL)isAnimating;
注意
在动画执行完毕后,需要清理缓存,把UIImageView赋值为nil
@interface ViewController () /** * 属性 */ @property (weak, nonatomic) IBOutlet UIImageView *tomImageView; /** * 喝牛奶 */ -(IBAction)drink; -(IBAction)cymbal; -(IBAction)fart; -(IBAction)pie; -(IBAction)scratch; -(IBAction)eat; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /** * 喝牛奶 */ -(IBAction)drink { [self runAnimationImagesWithFilePrefix: @"drink" andFileCount:80]; } -(IBAction)cymbal { [self runAnimationImagesWithFilePrefix: @"cymbal" andFileCount:12]; } -(IBAction)fart { [self runAnimationImagesWithFilePrefix: @"fart" andFileCount:27]; } -(IBAction)pie { [self runAnimationImagesWithFilePrefix: @"pie" andFileCount:23]; } -(IBAction)scratch { [self runAnimationImagesWithFilePrefix: @"scratch" andFileCount:55]; } -(IBAction)eat { [self runAnimationImagesWithFilePrefix: @"eat" andFileCount:39]; } /** * 播放动画 * * @param filePrefix 动画图片前缀 * @param count 动画图片张数 */ -(void)runAnimationImagesWithFilePrefix:(NSString*)filePrefix andFileCount:(NSInteger)count { NSInteger imageTotal = count; // 1.在动画播放时,不能点击 if([self.tomImageView isAnimating]){ return; } // 2.保存图片到数组 NSMutableArray *images = [NSMutableArray arrayWithCapacity:imageTotal]; for (int i=0; i<=imageTotal; i++) { NSBundle *bundle = [NSBundle mainBundle]; NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg",filePrefix,i]; [images addObject:[UIImage imageWithContentsOfFile:[bundle pathForResource:imageName ofType:nil]]]; } // 3.把数组赋值给tomImageView self.tomImageView.animationImages=images; // 4.播放动画 NSInteger delay = images.count*0.09; // 4.1设置播放次数 self.tomImageView.animationRepeatCount=1; // 4.2设置播放持续时间 self.tomImageView.animationDuration=delay; // 4.3开始播放 [self.tomImageView startAnimating]; // 5调用清空缓存 [self performSelector:@selector(clearCache) withObject:nil afterDelay:delay]; } /** * 清空缓存 */ -(void)clearCache { self.tomImageView.animationImages=nil; } @end
标签:style ar io color os 使用 sp for strong
原文地址:http://my.oschina.net/u/1032974/blog/357416