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()//停止