1.新建項(xiàng)目PhotoCollectionViewTest,在Main.stroyboard 的 ViewControllerScene 添加 NavigationControllerScene.
2.自定義cell。添加collectionViewCell.swift文件和collectionViewCell.xib文件。
import UIKit
class collectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageha: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
在collectionViewCell.xib里面添加UIColletionViewCell 和 UIImage.

jjj.png
ViewController 具體實(shí)現(xiàn)如下:
var collection: UICollectionView?
// 圖片數(shù)組
var dataArray: [UIImage] = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
// cell 尺寸
layout.itemSize = CGSizeMake(50, 50)
// cell 邊距距離
layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20)
collection = UICollectionView(frame: CGRectMake(0, 250, 375, 200), collectionViewLayout: layout)
collection?.dataSource = self
collection?.delegate = self
// 背景顏色
collection?.backgroundColor = UIColor.lightGrayColor()
self.view.addSubview(collection!)
// 創(chuàng)建 button
let button = UIButton(type: UIButtonType.System)
button.frame = CGRectMake(50, 500, 300, 50)
button.backgroundColor = UIColor.blueColor()
button.setTitle("添加圖片", forState: .Normal)
button.addTarget(self, action: "addImageAction", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
// 注冊xib,自定義cell
collection?.registerNib(UINib(nibName: "collectionViewCell",bundle: nil), forCellWithReuseIdentifier: "cell")
}
// 添加圖片
func addImageAction() {
// 創(chuàng)建圖片選擇器
let picker: UIImagePickerController = UIImagePickerController()
picker.delegate = self
// 創(chuàng)建提示框控制器
let actions: UIAlertController = UIAlertController(title: "溫馨提示", message: "請從手機(jī)相冊選擇照片或者拍照", preferredStyle: .ActionSheet)
// 提示框添加三個事件:1.相冊 2.拍照 3.取消操作
// 從相冊選擇
let action1: UIAlertAction = UIAlertAction(title: "從手機(jī)相冊選擇照片", style: .Default) { (action) -> Void in
// 推出圖片選擇器,默認(rèn)是相冊
self.navigationController?.presentViewController(picker, animated: true, completion: nil)
}
// 拍照操作
let action2: UIAlertAction = UIAlertAction(title: "拍照", style: .Default) { (action) -> Void in
// 調(diào)用相機(jī)之前判斷設(shè)備是否有攝像頭
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
// 將圖片選擇器的樣式設(shè)置成調(diào)用相機(jī)
picker.sourceType = .Camera
// 推出圖片選擇器
self.navigationController?.presentViewController(picker, animated: true, completion: nil)
} else {
print("不支持拍照") // 模擬器不支持拍照操作
}
}
// 取消操作
let action3: UIAlertAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
// 添加事件到控制器
actions.addAction(action1)
actions.addAction(action2)
actions.addAction(action3)
// 模態(tài)推出AlertController
self.navigationController?.presentViewController(actions, animated: true, completion: nil)
}
//UICollectionView 代理方法
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArray.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collection?.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! collectionViewCell
cell.imageha.image = dataArray[indexPath.item]
return cell
}
}
// UIImagePickerController 代理方法
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// 選擇照片
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image: UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
// 把圖片添加到數(shù)組
dataArray.append(image)
picker.dismissViewControllerAnimated(true) { () -> Void in
// 刷新界面
self.collection?.reloadData()
}
}
// 取消
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// 刪除圖片選擇器
picker.dismissViewControllerAnimated(true, completion: nil)
}
}
實(shí)現(xiàn)效果如下圖:

111.png

222.png

333.png