Swift图像控件(UIImageView)的用法

import UIKit

class ViewController: UIViewController        ,UITextFieldDelegate,UITextViewDelegate,UIActionSheet  Delegate,UIAlertViewDelegate{
var imageView2:UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()


  //1.使用图像控件显示图片
    let imageView  =  UIImageView(image: UIImage(named: "lalala"))
    imageView.frame = CGRectMake(10, 310, 200, 60)
    //改变图片
    imageView.image = UIImage(named: "111")
    self.view.addSubview(imageView)
    
    //2.从文件目录中获取图片
    let path = NSBundle.mainBundle().pathForResource("1", ofType: "png")
    let newImage = UIImage(contentsOfFile: path!)
    let imageView1 = UIImageView(image: newImage)
    imageView1.frame = CGRectMake(10, 380, 200, 60)
    self.view.addSubview(imageView1)
    
//        //3.从网络地址获取图片
//        //定义NSURL对象
//        let url = NSURL(string: "http://baidu.com/blog/images/logo.png")
//        //从网络获取数据流
//        let data = NSData(contentsOfURL: url!)
//        print(data)
//        //通过数据流初始化图片
//        let newImage1 = UIImage(data: data!)
//        
//        let imageView2 = UIImageView(image: newImage1)
//        
//        imageView2.frame = CGRectMake(10, 450, 200, 60)
//        
//        self.view.addSubview(imageView2)
    
    //4.使用图像控件实现动画播放
    imageView2 = UIImageView()
    imageView2.frame = CGRectMake(10, 520, 200, 60)
    //设置动画图片
    imageView2.animationImages = [UIImage(named: "1")!,UIImage(named: "111")!,UIImage(named: "lalala")!]
    //设置每隔0.5秒变换一次
    imageView2.animationDuration = 0.5
    //保持图片比例
    imageView2.contentMode = UIViewContentMode.ScaleToFill
    self.view.addSubview(imageView2)


 }

}

 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    imageView2.startAnimating()
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    imageView2.stopAnimating()
}

你可能感兴趣的:(Swift图像控件(UIImageView)的用法)