1.新建项目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.
ViewController 具体实现如下:
var collection: UICollectionView?
// 图片数组
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!)
// 创建 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() {
// 创建图片选择器
let picker: UIImagePickerController = UIImagePickerController()
picker.delegate = self
// 创建提示框控制器
let actions: UIAlertController = UIAlertController(title: "温馨提示", message: "请从手机相册选择照片或者拍照", preferredStyle: .ActionSheet)
// 提示框添加三个事件:1.相册 2.拍照 3.取消操作
// 从相册选择
let action1: UIAlertAction = UIAlertAction(title: "从手机相册选择照片", style: .Default) { (action) -> Void in
// 推出图片选择器,默认是相册
self.navigationController?.presentViewController(picker, animated: true, completion: nil)
}
// 拍照操作
let action2: UIAlertAction = UIAlertAction(title: "拍照", style: .Default) { (action) -> Void in
// 调用相机之前判断设备是否有摄像头
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
// 将图片选择器的样式设置成调用相机
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)
// 模态推出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
// 把图片添加到数组
dataArray.append(image)
picker.dismissViewControllerAnimated(true) { () -> Void in
// 刷新界面
self.collection?.reloadData()
}
}
// 取消
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// 删除图片选择器
picker.dismissViewControllerAnimated(true, completion: nil)
}
}
实现效果如下图: