代码之家  ›  专栏  ›  技术社区  ›  Daibaku

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

  •  0
  • Daibaku  · 技术社区  · 8 年前

    当用户使用DKImagePickerController选择图像时,我想在scrollview上显示多个图像 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)
                    }
    
                }
    
            }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   user8336100 user8336100    8 年前

    您可以将图像添加到数组中并重新加载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] 和预览视图: 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!
    }