     
     // select an asset at a specific index
     public func selectAsset(atIndex indexPath: IndexPath) {
-        guard let selectedAsset = (collectionView.cellForItem(at: indexPath) as? DKAssetGroupDetailBaseCell) else {
+        guard let cell = self.collectionView.cellForItem(at: indexPath) as? DKAssetGroupDetailBaseCell
+            , let asset = cell.asset else {
             return
         }
         
-        selectAsset(cellToSelect: selectedAsset)
-    }
-    
-    public func selectAsset(cellToSelect cell: DKAssetGroupDetailBaseCell) {
-        guard let asset = cell.asset else {
-            return
-        }
-        
-        self.imagePickerController.selectImage(asset)
-        cell.index = self.imagePickerController.selectedAssets.count - 1
-    }
-    
+        if !self.imagePickerController.contains(asset: asset) {
+            self.imagePickerController.select(asset: asset, updateGroupDetailVC: false)
+            cell.selectedIndex = self.imagePickerController.selectedAssetIdentifiers.count - 1
+            
+            if !cell.isSelected {
+                self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: [])
+            }
+        }
+    }
+    
+    public func deselectAsset(atIndex indexPath: IndexPath) {
+        guard let cell = self.collectionView.cellForItem(at: indexPath) as? DKAssetGroupDetailBaseCell
+            , let asset = cell.asset else {
+                return
+        }
+        
+        if let removedIndex = self.imagePickerController.index(of: asset) {
+            /// Minimize the number of times.
+            let indexPathsForSelectedItems = collectionView.indexPathsForSelectedItems!
+            let indexPathsForVisibleItems = collectionView.indexPathsForVisibleItems
+            
+            let intersect = Set(indexPathsForVisibleItems).intersection(Set(indexPathsForSelectedItems))
+            
+            for selectedIndexPath in intersect {
+                if let selectedCell = (collectionView.cellForItem(at: selectedIndexPath) as? DKAssetGroupDetailBaseCell)
+                    , let selectedCellAsset = selectedCell.asset
+                    , let selectedIndex = self.imagePickerController.index(of: selectedCellAsset) {
+                    if selectedIndex > removedIndex {
+                        selectedCell.selectedIndex = selectedCell.selectedIndex - 1
+                    }
+                }
+            }
+            
+            self.imagePickerController.deselect(asset: asset, updateGroupDetailVC: false)
+            
+            if cell.isSelected {
+                self.collectionView.deselectItem(at: indexPath, animated: true)
+            }
+        }
+    }
+
     func isCameraCell(indexPath: IndexPath) -> Bool {
         return indexPath.row == 0 && !self.hidesCamera
     }

                self.imagePickerController.presentCamera()
            }
        } else {
            selectAsset(atIndex: indexPath)
        }
    }
    
    public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        
        if let removedAsset = (collectionView.cellForItem(at: indexPath) as? DKAssetGroupDetailBaseCell)?.asset {
            let removedIndex = self.imagePickerController.selectedAssets.index(of: removedAsset)!
            
            /// Minimize the number of times.
            let indexPathsForSelectedItems = collectionView.indexPathsForSelectedItems!
            let indexPathsForVisibleItems = collectionView.indexPathsForVisibleItems
            
            let intersect = Set(indexPathsForVisibleItems).intersection(Set(indexPathsForSelectedItems))
            
            for selectedIndexPath in intersect {
                if let selectedCell = (collectionView.cellForItem(at: selectedIndexPath) as? DKAssetGroupDetailBaseCell), let selectedCellAsset = selectedCell.asset, let selectedIndex = self.imagePickerController.selectedAssets.index(of: selectedCellAsset) {
                    if selectedIndex > removedIndex {
                        selectedCell.index = selectedCell.index - 1
                    }
                }
            }
            
            self.imagePickerController.deselectImage(removedAsset)
        }
    }
    
    public func scrollViewDidScroll(_ scrollView: UIScrollView) {

    private var currentViewSize: CGSize!
    private var registeredCellIdentifiers = Set<String>()
    private var thumbnailSize = CGSize.zero
    private weak var gallery: DKPhotoGallery?
	
	override open func viewWillLayoutSubviews() {
		super.viewWillLayoutSubviews()
