简体   繁体   English

通过使用DKImagePickerController在scrollview上显示多个图像

[英]Display multiple images on scrollview by using DKImagePickerController

I would like to display multiple images on scrollview when user selected images with DKImagePickerController github . 当用户使用DKImagePickerController github选择图像时,我想在scrollview上显示多个图像。 Here is my code but images don't appear. 这是我的代码,但没有出现图像。 Anyone can tell what's wrong? 任何人都可以说出什么问题了吗? Thank you in advance! 先感谢您!

    var picker = DKImagePickerController()
    var imagesArray = [Any]()

     @IBAction func pickPhoto(_ sender: Any) {

            picker.maxSelectableCount = 10
            picker.showsCancelButton = true
            picker.allowsLandscape = false
            picker.assetType = .allPhotos

            self.present(picker, animated: true)

            picker.didSelectAssets = { (assets: [DKAsset]) in

                self.imagesArray.append(assets)

                for i in 0..<self.imagesArray.count {

                    let imageView = UIImageView()
                    imageView.image = self.imagesArray[i] as? UIImage
                    imageView.contentMode = .scaleAspectFit
                    let xposition = self.view.frame.width * CGFloat(i)
                    imageView.frame = CGRect(x: xposition, y: 330, width: self.scrollView.frame.width, height: 170)
                    self.scrollView.contentSize.width = self.scrollView.frame.width * CGFloat(i * 1)
                    self.scrollView.addSubview(imageView)
                }

            }

        }

Instead of displaying images to scrollView, you can add that images in array and reload the collectionView/TableView.. 可以将图像添加到数组中并重新加载collectionView / TableView。

as I did it here in collectionView 就像我在collectionView中所做的一样

func showImagePicker() {
    let pickerController = DKImagePickerController()
    self.previewView?.isHidden = false
    pickerController.assetType = .allPhotos
    pickerController.defaultAssetGroup = .smartAlbumUserLibrary
    pickerController.didSelectAssets = { (assets: [DKAsset]) in
        if  assets.count>0
        {
            for var i in 0 ... assets.count-1 {
                assets[i].fetchOriginalImage(true, completeBlock: { (image, info) in
                        self.abc.append(image)
                })
            }
        }
        self.previewView?.reloadData()
    }
    self.present(pickerController, animated: true) {}
}

Here abc : [UIImage] and previewView? abc: [UIImage]和PreviewView? : UICollectionView and in collection delegate methods: UICollectionView和集合委托方法中:

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    return self.abc.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var cell: UICollectionViewCell?
    var imageView: UIImageView?

    cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellImage", for: indexPath)
    imageView = cell?.contentView.viewWithTag(1) as? UIImageView
    if let cell = cell, let imageView = imageView
    {
        let tag = indexPath.row + 1
        cell.tag = tag
        imageView.image = self.abc[indexPath.row]
    }
    return cell!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM