1,使用图像控件显示图片
2,改变图片
3,从文件目录中获取图片
4,从网络地址获取图片
5,使用图像控件实现动画播放 UIImageView中提供了存储多张图片来创建动画的功能,具体做法是,在animationImages属性中设置一个图片数组,然后使用 startAnimating方法开始动画,最后用stopAnimating方法停止动画。同时,使用animationDuration属性中可以设 置动画每帧切换的速度(秒)。
1 2 3 | var imageView= UIImageView (image: UIImage (named: "icon" )) imageView.frame= CGRectMake (10,10,100,100) self .view.addSubview(imageView) |
1 | imageView.image= UIImage (named: "icon2" ) |
1 2 3 4 | let path = NSBundle .mainBundle().pathForResource( "apple" , ofType: "png" ) var newImage = UIImage (contentsOfFile: path!) var imageView = UIImageView (image:newImage) self .view.addSubview(imageView) |
1 2 3 4 5 6 7 8 | //定义NSURL对象 let url = NSURL (string: "" ) //从网络获取数据流 var data = NSData (contentsOfURL: url!) //通过数据流初始化图片 var newImage = UIImage (data: data!) var imageView = UIImageView (image:newImage); self .view.addSubview(imageView) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class ViewController : UIViewController { var imageView: UIImageView ! override func viewDidLoad() { super .viewDidLoad() imageView= UIImageView () imageView.frame= CGRectMake (10,10,100,100) //设置动画图片 imageView.animationImages=[ UIImage (named: "icon" )!, UIImage (named: "icon2" )!] as [ AnyObject ] //设置每隔0.5秒变化一次 imageView.animationDuration=0.5 self .view.addSubview(imageView) } override func viewWillAppear(animated: Bool ) { super .viewWillAppear(animated) imageView.startAnimating() } override func viewWillDisappear(animated: Bool ) { super .viewWillAppear(animated) imageView.stopAnimating() } } |