最近要做一个iOS相机及相册图片上传,其中遇到了这些问题:1、图片增删在UICollectionView里的变化;2、获取相机拍摄的照片和相册的照片;3、将PHAsset对象转为UIImage对象;
具体实现过程:
首先整个控件结构如图所示:
storyboard长这样:
拖动三个UIViewController如图所示,其中:AlbumImageCollectionViewController(id为ImageCollectionVC)中拖入一个UICollectionView、一个UIToolBar,UICollectionView中的cell(id为cell)拖入两个UIImageView;AlbumImagePickerViewController(id为imagePickerVC)中拖入一个UITableView,UITableView的cell(id为cell)拖入两个UILabel。
PhotoCollectionViewCell:
import UIKit
// 已选择的图片缩略图cell
class PhotoCollectionViewCell: UICollectionViewCell {
// 已选择的图片
var imageView: UIImageView!
// 删除按钮
var button = UIButton()
// MARK: - 初始化方法
override init(frame: CGRect) {
super.init(frame: frame)
// 设置缩略图大小
imageView = UIImageView(frame: self.bounds)
// 设置按钮属性
button = UIButton(type: UIButtonType.custom)
button.setImage(UIImage.init(named: "delete"), for: UIControlState.normal)
button.frame = CGRect(x: frame.size.width - 21, y: 1, width: 20, height: 20)
self.addSubview(imageView)
self.addSubview(button)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
AlbumImagePickerTableViewCell:
import UIKit
// 相簿列表单元格
class AlbumImagePickerTableViewCell: UITableViewCell {
// 相簿名称标签
@IBOutlet weak var titleLabel: UILabel!
// 照片数量标签
@IBOutlet weak var countLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
self.layoutMargins = UIEdgeInsets.zero
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
AlbumImageCollectionViewCell:
import UIKit
// 图片缩略图集合页单元格
class AlbumImageCollectionViewCell: UICollectionViewCell {
// 显示缩略图
@IBOutlet weak var imageView: UIImageView!
// 显示选中状态的图标
@IBOutlet weak var selectedIcon: UIImageView!
// 设置是否选中
open override var isSelected: Bool {
didSet{
if isSelected {
selectedIcon.image = UIImage(named: "image_selected")
}else{
selectedIcon.image = UIImage(named: "image_not_selected")
}
}
}
// 播放动画,是否选中的图标改变时使用
func playAnimate() {
// 图标先缩小,再放大
UIView.animateKeyframes(withDuration: 0.4, delay: 0, options: .allowUserInteraction, animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.2, animations: {
self.selectedIcon.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
})
UIView.addKeyframe(withRelativeStartTime: 0.2, relativeDuration: 0.4, animations: {
self.selectedIcon.transform = CGAffineTransform.identity
})
}, completion: nil)
}
override func awakeFromNib() {
super.awakeFromNib()
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
}
}
AlbumImageCompleteButton:
import UIKit
// 照片选择页下方工具栏的“完成”按钮
class AlbumImageCompleteButton: UIView {
// 已选照片数量标签
var numLabel: UILabel!
// 按钮标题标签“完成”
var titleLabel: UILabel!
// 按钮的默认尺寸
let defaultFrame = CGRect(x: 0, y: 0, width: 70, height: 20)
// 文字颜色(同时也是数字背景颜色)
let titleColor = UIColor(red: 0x09/255, green: 0xbb/255, blue: 0x07/255, alpha: 1)
// 点击点击手势
var tapSingle: UITapGestureRecognizer?
// 设置数量
var num:Int = 0{
didSet{
if num == 0{
numLabel.isHidden = true
}else{
numLabel.isHidden = false
numLabel.text = "\(num)"
playAnimate()
}
}
}
//是否可用
var isEnabled:Bool = true {
didSet{
if isEnabled {
titleLabel.textColor = titleColor
tapSingle?.isEnabled = true
}else{
titleLabel.textColor = UIColor.gray
tapSingle?.isEnabled = false
}
}
}
init(){
super.init(frame:defaultFrame)
// 已选照片数量标签初始化
numLabel = UILabel(frame:CGRect(x: 0 , y: 0 , width: 20, height: 20))
numLabel.backgroundColor = titleColor
numLabel.layer.cornerRadius = 10
numLabel.layer.masksToBounds = true
numLabel.textAlignment = .center
numLabel.font = UIFont.systemFont(ofSize: 15)
numLabel.textColor = UIColor.white
numLabel.isHidden = true
self.addSubview(numLabel)
// 按钮标题标签初始化
titleLabel = UILabel(frame:CGRect(x: 20 , y: 0 ,
width: defaultFrame.width - 20,
height: 20))
titleLabel.text = "完成"
titleLabel.textAlignment = .center
titleLabel.font =