swift ImagePicker,第一次迭代

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift ImagePicker,第一次迭代相关的知识,希望对你有一定的参考价值。

import UIKit
import Kingfisher

protocol ImagPickerProtocol {
    var onImageClick: ((_ collectionView: UICollectionView, _ indexPath: IndexPath) -> ())? {get set}
}

class ImagePicker: UICollectionView, ImagPickerProtocol {
    
    var onImageClick: ((_ collectionView: UICollectionView, _ indexPath: IndexPath) -> ())?
    var images = [String]()
    var max = 5
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        setupSelf()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupSelf()
    }
    
    private func setupSelf() {
        backgroundColor = UIColor.white
        dataSource = self
        delegate = self
    }
}

extension ImagePicker: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "pictureCell", for: indexPath) as! PictureCell
        let size = images.count
        if (indexPath.row == size) { cell.backgroundColor = UIColor.gray }
        else {
            let url = URL(string: images[indexPath.row])
            cell.picture.kf.setImage(with: url)
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let size = images.count
        if (size < max) { return size + 1 }
        return size
    }
}

extension ImagePicker: UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        onImageClick?(collectionView, indexPath)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 100, height: 100)
    }
}
import UIKit

class CreateOfferViewController: BaseViewController, CreateOfferViewInput {
    
    var imagePicker: ImagePicker?
    var viewBuilder: ViewBuilder?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        imagePicker = ImagePicker(frame: CGRect(x: 0, y: 16, width: view.bounds.width, height: 100), collectionViewLayout: layout)
        imagePicker?.onImageClick = { [weak self] (collectionView, indexPath) in
            let ipk = UIImagePickerController()
            self?.present(ipk, animated: true, completion: nil)
        }
        view.addSubview(imagePicker!)
        imagePicker?.register(PictureCell.self, forCellWithReuseIdentifier: "pictureCell")
    
        imagePicker!.images = [
            "https://firebasestorage.googleapis.com/v0/b/samuiplus-9d4b0.appspot.com/o/categories%2Foffers%2F-L7ZLum7uwZ7IAS1gP0n%2F-L7bM1f2aYGGwLNCDSNr%2Fimg_1518014663757.jpg?alt=media&token=34095b90-a53a-4413-8e67-f5633d154c1f",
            "https://firebasestorage.googleapis.com/v0/b/samuiplus-9d4b0.appspot.com/o/categories%2Foffers%2F-L85HnRFlWJJwZeqHtLn%2F%D0%A1%D1%80%D0%B5%D0%B4%D0%B8-%D1%80%D0%B8%D1%81%D0%BE%D0%B2%D1%8B%D1%85-%D0%BF%D0%BE%D0%BB%D0%B5%D0%B8%CC%86-%D0%BE.-%D0%91%D0%B0%D0%BB%D0%B8-%D0%98%D0%BD%D0%B4%D0%BE%D0%BD%D0%B5%D0%B7%D0%B8%D1%8F.jpg?alt=media&token=96f4fbec-ca21-47ec-a776-af9551413c1a",
            "https://firebasestorage.googleapis.com/v0/b/samuiplus-9d4b0.appspot.com/o/categories%2Foffers%2F-L85H0HTGT6s704K8qpt%2Fimages%20(62).jpg?alt=media&token=5b9c2537-2809-45a0-8a7d-48294ae974ca"]
    }
}

// MARK: Button Action
extension CreateOfferViewController {
    
}
import UIKit

class PictureCell: UICollectionViewCell {
    
    var picture = UIImageView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initPicture()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initPicture()
    }
    
    private func initPicture() {
        picture.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
        picture.contentMode = .center
        picture.clipsToBounds = true
        picture.backgroundColor = UIColor.black
        self.addSubview(picture)
    }
}
import UIKit

class ImagePickerViewController: BaseCollectionViewController, ImagePickerViewInput, ImagPickerProtocol {
    
    var onImageClick: ((_ collectionView: UICollectionView, _ indexPath: IndexPath) -> ())?
    var images = [String]()
    var max = 5
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

// MARK: UICollectionViewDataSource
extension ImagePickerViewController {
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "pictureCell", for: indexPath) as! PictureCell
        let size = images.count
        if (indexPath.row == size) { cell.backgroundColor = UIColor.gray }
        else {
            let url = URL(string: images[indexPath.row])
            cell.picture.kf.setImage(with: url)
        }
        return cell
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let size = images.count
        if (size < max) { return size + 1 }
        return size
    }
}

// MARK: UICollectionViewDelegate
extension ImagePickerViewController {
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        onImageClick?(collectionView, indexPath)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 100, height: 100)
    }
}

以上是关于swift ImagePicker,第一次迭代的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Swift/SwiftUI 从 ImagePicker 获取选定的图像文件名

iOS Swift:在呈现 imagePicker 后,SlidingMenuView 位置错误

Swift ImagePicker VideoCapture didFInishPickingMedia 未被调用

UIImagePickerController 委托未调用 Swift 3

UIImagePickerController委托不叫Swift 3

swift 1.2 中的 JSON 解析问题