如果要将图片放到程序中要用到UIImageView
UIImageView 和 UIImage:
UIImage:
方法一:[小图片,重复利用]
方法二:[大图片,不重复]
UIImageView 常用属性:
UIImageView 动画:
//
// ccyAppDelegate.m
// UIImageViewDemo
//
// Created by ccy on 13-12-26.
// Copyright (c) 2013年 ccy. All rights reserved.
//
#import "ccyAppDelegate.h"
@implementation ccyAppDelegate
{
UIImageView * _imageView1;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIImage * image = [UIImage imageNamed:@"57x57.png"];
//UIImage * image = [UIImage imageNamed:@"test.jpg"];
UIImageView * imageView = [[UIImageView alloc] initWithImage:image];
//设置显示区域大小 == 图片大小
//imageView.frame = CGRectMake(10, 20, image.size.width , image.size.height);
//设置固定显示区域大小
imageView.frame = CGRectMake(10, 30, 300, 100);
imageView.backgroundColor = [UIColor redColor];
//设置内容模式 contentMode 是UIView属性
/*
UIViewContentModeCenter - 图片不被拉伸,居中显示
UIViewContentModeScaleToFill, - 将图片拉伸(比例可能改变),充满整个屏幕
UIViewContentModeScaleAspectFit, - 将图片拉伸(比例不变),小的一边充满ImageView,大的一边可能超出ImageView的frame
// contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill, - 将图片拉伸(比例不变),大的一边充满ImageView,小的一边可能超出ImageView的frame
// contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
每次改变frame都会调用这个方法
*/
imageView.contentMode = UIViewContentModeRedraw;
[self.window addSubview:imageView];
//[imageView release];
/*图片动画*/
NSMutableArray * images = [[NSMutableArray alloc] init];
for (int i=1; i<=9; i++) {
UIImage * image1 = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
[images addObject:image1];
}
_imageView1 = [[UIImageView alloc] init];
_imageView1.frame = CGRectMake(10, 150, 300, 100);
//设置动画数组 (图片数组)
_imageView1.animationImages = images;
//设置动画周期 (全部播放需要的时间)单位 s
_imageView1.animationDuration = 1;
//设置动画播放次数 (缺省:无限播放)
//_imageView1.animationRepeatCount = 5;
[self.window addSubview:_imageView1];
//[_imageView1 startAnimating];
//动画结束按钮
UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(5, 260, 90, 20);
btn1.tag = 1;
[btn1 setTitle:@"停止动画" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn1];
//动画检测状态按钮
UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = CGRectMake(100, 260, 90, 20);
btn2.tag = 2;
[btn2 setTitle:@"动画状态" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn2];
//动画启动按钮
UIButton * btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn3.frame = CGRectMake(195, 260, 90, 20);
btn3.tag = 3;
[btn3 setTitle:@"开启动画" forState:UIControlStateNormal];
[btn3 addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn3];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)btnclick:(id)arg
{
UIButton * btn = (UIButton *)arg;
switch (btn.tag) {
case 1:
//结束
[_imageView1 stopAnimating];
break;
case 2:
//检测
if([_imageView1 isAnimating])
NSLog(@"动画正在运行");
else
NSLog(@"动画停止运行");
break;
case 3:
//启动
[_imageView1 startAnimating];
break;
default:
break;
}
}