本文介绍照片的显示与移除,并介绍CollectionView的使用(SmartAgency CreatePdf)
let _layout = UICollectionViewFlowLayout()
var pictureArray = [PdfModel]()
1 在ViewDidLoad中调用CollectionView的方法
override func viewDidLoad(){
super.viewDidLoad()
self.configCollectionView()
}
func configCollectionView(){
//collectionView初始化
self.collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: _layout)
let rgb = 244 / 255.0;
//横竖屏设置
collectionView.alwaysBounceVertical = true
collectionView.backgroundColor = UIColor.init(colorLiteralRed: Float(rgb), green: Float(rgb), blue: Float(rgb), alpha: 1.0)
collectionView.contentInset = UIEdgeInsetsMake(4, 4, 4, 4)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.keyboardDismissMode = .onDrag
self.topView.addSubview(collectionView)
collectionView.register(TZTestCell.self, forCellWithReuseIdentifier: "TZTestCell")
}
override func viewDidLayoutSubviews(){
super.viewDidLayoutSubviews()
let contentSizeH = 20
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.scrollView.contentSize = CGSize.init(width: 0, height: contentSizeH + 5)
}
let _margin:CGFloat = 4
_layout.itemSize = CGSize.init(width:45, height:45)
_layout.minimumInteritemSpacing = CGFloat(_margin);
_layout.minimumLineSpacing = CGFloat(_margin);
self.collectionView.setCollectionViewLayout(_layout, animated: true)
// 设置CollectionView的大小
self.collectionView.frame = CGRect.init(x: 0, y: 0, width:self.topView.frame.width - 10 , height: self.topView.frame.height - 20)
}
设置CollectionView的代理方法
@available(iOS 6.0, *)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TZTestCell", for: indexPath) as!TZTestCell
cell.videoImageView.isHidden = true
if(indexPath.row == pictureArray.count){
//cell.imageView.image = _selectedPhotos[indexPath.row]
cell.imageView.image = UIImage.init(named: "AlbumAddBtn.png")
cell.deleteBtn.isHidden = true
cell.gifLable.isHidden = true
} else{
let model = pictureArray[indexPath.row]
cell.imageView.image = model.imgName
cell.deleteBtn.isHidden = false
}
cell.deleteBtn.tag = indexPath.row
cell.deleteBtn.addTarget(self, action:#selector(deleBtnClick(_:)), for: .touchUpInside)
return cell
}
@available(iOS 6.0, *)
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pictureArray.count + 1;
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
print("111111111111111111111111111111111111")
if (indexPath.row == pictureArray.count){
self.pushTZImagePickerController()
}else{
let model = pictureArray[indexPath.row]
let photos = model.imgName
let imagePickerVC = TZImagePickerController.init(selectedAssets: pictureArray as! NSMutableArray, selectedPhotos: pictureArray as! NSMutableArray, index: indexPath.row)
imagePickerVC?.didFinishPickingPhotosHandle = { [weak self] (photos, assets, isSelectOriginalPhoto) in
self?.pictureArray = NSMutableArray.init(array: (self?.pictureArray)!) as! [PdfModel]
collectionView.reloadData()
collectionView.contentSize = CGSize.init(width: CGFloat(0), height: (CGFloat((self?.pictureArray.count)!) + CGFloat(2))/CGFloat(3) * (CGFloat((self?._margin)!) + CGFloat((self?._itemWH)!)))
self?.present(imagePickerVC!, animated: true, completion: nil)
}
}
}
func pushTZImagePickerController(){
let imagePickVC = TZImagePickerController.init(maxImagesCount: 1, delegate: self)
imagePickVC?.didFinishPickingPhotosHandle = { [weak self] (photos, assets, isSelectOriginalPhoto) in
let image = photos?.first
self?.imageData = UIImageJPEGRepresentation(image!, 0.3) //压缩
let tempImg = UIImage.init(data: (self?.imageData!)!)
let fileManager = FileManager.default
//let filePath = String.
//拿到本地的documents路径
let docPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
let filePath = "\(docPath!)/image1"
if (!fileManager.fileExists(atPath: filePath)){
try? fileManager.createDirectory(atPath: filePath, withIntermediateDirectories: true, attributes: nil)
}
let uuid = UUID.init()
let imageNamePath = "\(filePath)/\(uuid).png"
try? fileManager.createFile(atPath: imageNamePath, contents: self?.imageData, attributes: nil)
let pdfModel = PdfModel()
pdfModel.imgPath = imageNamePath
pdfModel.imgName = tempImg
self?.pictureArray.append(pdfModel)
DispatchQueue.main.async {
self?.collectionView.reloadData()
}
}
//第三方封装号打开相册
self.present(imagePickVC!, animated: true, completion: nil)
}
//collection完成选择图片后的方法
func deleBtnClick(_ btn:UIButton){
pictureArray.remove(at: btn.tag)
collectionView.performBatchUpdates({
let indexPath = IndexPath.init(row: btn.tag, section: 0)
self.collectionView.deleteItems(at: [indexPath])
}) { (finished) in
self.collectionView.reloadData()
}
}
//向照片数组中添加照片,刷新CollectionView
func refreshCollectionViewWithAddedAsset(image:UIImage){
self.pdfModel.imgName = image
self.pictureArray.append(self.pdfModel)
collectionView.reloadData()
}
//CollectionView通过Layout 方法布局CollectionViewCell 所在的位置
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width - 1, height: 650 * (collectionView.frame.width - 1)/(842) )
}