UIImageView

创建ImageView

let imageView = UIImageView(frame: CGRectMake(10, 100, self.view.frame.size.width-20, 400))
imageView.backgroundColor = UIColor.orangeColor()
imageView.tag = 101
imageView.image = UIImage(named: "0")
imageView.contentMode = .ScaleAspectFit//保持图片比例
self.view.addSubview(imageView)

从文件中获取图片

let path = NSBundle.mainBundle().pathForResource("1", ofType: "png")
if let newPath = path{
    imageView.image = UIImage(contentsOfFile: newPath)
}

从网络获取图片

let url = NSURL(string: "http://ww2.sinaimg.cn/bmiddle/632dab64jw1ehgcjf2rd5j20ak07w767.jpg")
let data = NSData(contentsOfURL: url!)
if let newData = data {
    imageView.image = UIImage(data: newData)
}

多张图片播放动画

let image1 = UIImage(named:"0")!
let image2 = UIImage(contentsOfFile: path!)!
let image3 = UIImage(data: data!)!
var array  = [UIImage]()
array.append(image1)
array.append(image2)
array.append(image3)

imageView.animationImages = array
imageView.animationDuration = 1
imageView .startAnimating()//开始
imagev.stopAnimating()//停止

  

来源: http://www.cnblogs.com/spaceID/p/4976689.html 

posted @ 2015-11-19 10:00  寻找小猪  阅读(136)  评论(0编辑  收藏  举报