// // ShowFullPicController.swift // PaiAi // // Created by zhengjianfei on 16/4/9. // Copyright © 2016年 FFIB. All rights reserved. // import UIKit import PaiaiDataKit import PaiaiUIKit final class ShowFullPicController: UIViewController { // MARK: Storyboard property @IBOutlet var collectionView: UICollectionView! // @IBOutlet weak var progressView: FFProgress! // MARK: parameter property lazy var datas = [PhotoItem]() lazy var currentPhotoIndex = 0 lazy var firstLayout = true lazy var currentPageIndex = 0 lazy var showNomark = false lazy var showHD = false var shufflingImage = [String]() // MARK: Controller fucntion override func viewDidLoad() { super.viewDidLoad() navigationController?.isNavigationBarHidden = true // titleWithbackBar = "" } override func viewDidLayoutSubviews() { if firstLayout { collectionView.contentOffset = CGPoint(x: (CGFloat(currentPhotoIndex) * (collectionView.width)), y: 0) firstLayout = false } super.viewDidLayoutSubviews() } // MARK: Storyboard button function @IBAction func back() { _ = navigationController?.popViewController(animated: true) guard let ctl = navigationController?.viewControllers.last as? DetailPageController else { return } ctl.currentPhotoIndex = currentPageIndex ctl.tableView.reloadData() } @IBAction func rotateTheImage(_ sender: UIButton) { guard let cell = collectionView.cellForItem(at: IndexPath(item: currentPageIndex, section: 0)) as? ImageCell else { return } UIView.beginAnimations("image.rotate", context: nil) UIView.animate(withDuration: 0.5) { let image = cell.photoImage.image switch image?.imageOrientation.rawValue { case .some(0): cell.photoImage.image = UIImage(cgImage: (image?.cgImage)!, scale: UIScreen.main.scale, orientation: UIImage.Orientation.right) case .some(1): cell.photoImage.image = UIImage(cgImage: (image?.cgImage)!, scale: UIScreen.main.scale, orientation: UIImage.Orientation.left) case .some(2): cell.photoImage.image = UIImage(cgImage: (image?.cgImage)!, scale: UIScreen.main.scale, orientation: UIImage.Orientation.up) case .some(3): cell.photoImage.image = UIImage(cgImage: (image?.cgImage)!, scale: UIScreen.main.scale, orientation: UIImage.Orientation.down) default: break } } } @IBAction func load() { guard let cell = collectionView.cellForItem(at: IndexPath(item: currentPageIndex, section: 0)) as? ImageCell else { // FFToastView.showToast(inView: view, withText: "未检测到图片") return } guard let image = cell.photoImage.image else { // FFToastView.showToast(inView: view, withText: "未检测到图片") return } UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) } @objc func image(_ image: UIImage?, didFinishSavingWithError error: NSError?, contextInfo info: UnsafeMutableRawPointer) { if error != nil { // FFToastView.showToast(inView: view, withText: "保存图片失败") } else { // FFToastView.showImageToast(inView: view, withText: "已保存图片到相册", withImage: "提示弹窗-勾") } } } // MARK: UICollectionView delegate extension ShowFullPicController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return shufflingImage.count > datas.count ? shufflingImage.count : datas.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCell if shufflingImage.count <= 0 { let data = datas[indexPath.item] let urlStr = data.murl.isEmpty ? data.photo_url : data.murl cell.setModel(url: urlStr) } else { cell.setModel(url: shufflingImage[indexPath.row]) } return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: collectionView.width, height: collectionView.height - 20) } func scrollViewDidScroll(_ scrollView: UIScrollView) { let page = Int(scrollView.contentOffset.x / (collectionView.width)) currentPageIndex = page } }