添加到阵列

时间:2016-02-25 18:09:46

标签: ios swift image

我正在使用DKImagePickerController来选择多个图像,当我按下他们上传到Parse的按钮时,将它们显示在我的VC上。

我加载了7张图片,但只有屏幕上可见的图片(这是4,因为我正在使用带水平滚动的collectionView)在开头上传然后如果我滚动到最后并再次上传它上传所有7张图片。如果我向后滚动然后再向前滚动,图像似乎一次加起来7,所以如果我来回滚动4次,当我只选择7时,我最终会上传28张图像!

我猜这与我将我选择的图像添加到我在cellForItemAtIndexPath中执行的图像数组的方式有关。

以下是我的代码,如果有人事先知道这个问题,或者知道你提供帮助的问题,我们非常感谢!

    @IBOutlet weak var previewOrShareButton: UIBarButtonItem!
   @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var cancelButton: UIBarButtonItem!

    var assets: [DKAsset]?

    var images = [UIImage]()

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func selectImage(sender: AnyObject) {
        let pickerController = DKImagePickerController()

        pickerController.didSelectAssets = { (assets: [DKAsset]) in
            print("didSelectAssets")
            self.assets = assets

           self.collectionView.reloadData()
        }
        pickerController.defaultSelectedAssets = self.assets
        pickerController.maxSelectableCount =  7
        pickerController.allowMultipleTypes = false

        self.presentViewController(pickerController, animated: true) {}


    }


    @IBAction func postButton(sender: AnyObject) {

        let post = PFObject(className: "Post")

        post["username"] = PFUser.currentUser()!.username

        for i in self.images.indices {
            let imageData = self.images[i].lowestQualityJPEGNSData 
            let imageFile = PFFile(name: "image.JPEG", data: imageData)

        post["imageArray\(i)"] = imageFile

        }


        post.saveInBackgroundWithBlock ({(success:Bool, error:NSError?) -> Void in
            if error  == nil {
            }})
     }

 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return self.assets?.count ?? 0
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! BlogCollectionViewCell
        let asset = self.assets![indexPath.row]



        asset.fetchOriginalImageWithCompleteBlock { (image, info) -> Void in

           cell.image.image = image

          self.images.append(image!)
        }

        print(images)

        return cell
    }

2 个答案:

答案 0 :(得分:1)

是的,你是以错误的方式做的,因为你在cellForItemAtIndexPath中获取图像,然后以相同的方法在数组中添加图像。因此,当您滚动UICollectionView时,图像将被提取并添加到数组中,因为当您滚动或重新加载UICollectionView时将调用cellForItemAtIndexPath。

我会对您的代码进行一些更改,希望它能为您提供帮助。

@IBAction func selectImage(sender: AnyObject) {

    let pickerController = DKImagePickerController()

    pickerController.didSelectAssets = { (assets: [DKAsset]) in
        print("didSelectAssets")
        assets.fetchOriginalImageWithCompleteBlock { (image, info) -> Void in
           self.images.append(image!)
           self.collectionView.reloadData()
        }
    }
    pickerController.defaultSelectedAssets = self.assets
    pickerController.maxSelectableCount =  7
    pickerController.allowMultipleTypes = false
    self.presentViewController(pickerController, animated: true) {}
}

然后cellForItemAtIndexPath将是

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! BlogCollectionViewCell
    let asset = self.assets![indexPath.row]
    cell.image.image = image
    return cell
}

答案 1 :(得分:1)

每次滚动单元格时,cellForRow中的任何内容都会运行,因此它会获取太多图像。理想情况下,将以下内容添加到viewDidLoad中,除非您需要用户下载图像,然后将其添加到按钮中:

for object in assets {
    object.fetchOriginalImageWithCompleteBlock { (image, info) -> Void in

      self.images.append(image!)
      pickerController.reloadData()

    }
}

这会将所有图像加载到数组中,然后您可以使用图像填充单元格:

cell.image = images[indexPath.row]