Geen omschrijving

PhotoPreviewViewController.swift 5.0KB

    // // PhotoPreviewViewController.swift // PaiAi // // Created by zhengjianfei on 16/4/9. // Copyright © 2016年 FFIB. All rights reserved. // import UIKit import PaiaiDataKit import PaiaiUIKit import RxCocoa import RxSwift import RxDataSources final class PhotoPreviewViewController: UIViewController { /// MARK: Storyboard property @IBOutlet weak var collectionView: UICollectionView! var viewModel: PhotoDetailListViewModel! var disposeBag = DisposeBag() override var prefersStatusBarHidden: Bool { return true } override func viewDidLoad() { super.viewDidLoad() binding() scrollToSpecifiedImage() navigationController?.setNavigationBarHidden(true, animated: true) } func scrollToSpecifiedImage() { collectionView.layoutIfNeeded() collectionView.scrollToItem(at: IndexPath(item: viewModel.currIndex, section: 0), at: .right, animated: false) } override func viewDidAppear(_ animated: Bool) { super.viewWillAppear(animated) bindCollectionViewToViewModel() } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) navigationController?.setNavigationBarHidden(false, animated: true) } @IBAction func download(_ sender: UIButton) { guard let cell = collectionView.cellForItem(at: IndexPath(item: viewModel.currIndex, section: 0)) as? ImageCell, 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: "提示弹窗-勾") } } } /// binding extension PhotoPreviewViewController { var dataSource: RxCollectionViewSectionedAnimatedDataSource<AnimatableSectionModel<Int, PhotoItem>> { return RxCollectionViewSectionedAnimatedDataSource<AnimatableSectionModel<Int, PhotoItem>>(configureCell: { (dataSource, collectionView, indexPath, item) in let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCell cell.setModel(url: item.murl.isEmpty ? item.photo_url : item.murl) return cell }) } func binding() { bindViewModelToCollectionView() bindCollectionViewDelegate() } func bindViewModelToCollectionView() { viewModel.content .bind(to: collectionView.rx.items(dataSource: dataSource)) .disposed(by: disposeBag) } func bindCollectionViewDelegate() { collectionView.rx.setDelegate(self).disposed(by: disposeBag) } func bindCollectionViewToViewModel() { collectionView.rx.willDisplayCell .asDriver() .drive(onNext: { [unowned self] in self.viewModel.willShow(index: $0.at.row) }) .disposed(by: disposeBag) } } /// storyboard button action extension PhotoPreviewViewController { @IBAction func back() { navigationController?.popViewController(animated: true) } @IBAction func rotateTheImage(_ sender: UIButton) { guard let cell = collectionView.cellForItem(at: IndexPath(item: viewModel.currIndex, 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 } } } } // MARK: UICollectionView delegate extension PhotoPreviewViewController: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: collectionView.width, height: collectionView.height - 20) } }