【问题标题】:Display multiple images on scrollview by using DKImagePickerController使用 DKImagePickerController 在滚动视图上显示多个图像
【发布时间】:2017-12-05 08:14:49
【问题描述】:

当用户使用 DKImagePickerController github 选择图像时,我想在滚动视图上显示多个图像。 这是我的代码,但没有出现图像。任何人都可以告诉什么是错的? 提前谢谢!

    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)
                }

            }

        }

【问题讨论】:

    标签: ios swift xcode uiscrollview


    【解决方案1】:

    您可以将图像添加到数组中并重新加载collectionView/TableView..

    ,而不是将图像显示到scrollView

    就像我在 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) {}
    }
    

    这里是 abc :[UIImage] 和 previewView? :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!
    }
    

    【讨论】:

    • @Daibaku 让我知道这个答案是否对您有帮助!
    • 谢谢你的回答!!我希望图像像带有页面控制的 instagram。因此,如果用户选择了 10 张照片,则显示这些照片并使用页面控件水平滚动。用collectionview可以吗?
    • @Daibaku 如果你觉得这个答案有帮助,请点赞并标记为关于你的问题的正确 nd:: 是的,我们可以在 collectionView 中启用分页,更多像 instagram 你可以使用 ImageSlideShow github (github.com/zvonicek/ImageSlideshow)通过在数组中使用 for 循环来简化代码
    • 非常感谢,我回家试试!!再次感谢您:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2011-10-23
    • 1970-01-01
    • 2019-02-08
    相关资源
    最近更新 更多