MWPhotoBrowser - 如何实现删除照片功能?

时间:2014-01-21 19:58:01

标签: photo mwphotobrowser

我正在为我的应用使用MWPhotoBrowser。我需要为用户提供删除功能。我们有什么方法可以实现删除特定照片或多张照片功能吗?

需要快速帮助。

1 个答案:

答案 0 :(得分:3)

enter image description here

我在swift中通过从库外添加此扩展来完成此操作:

extension MWPhotoBrowser {

    public override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        if let _ = valueForKey("_gridController") as? MWGridViewController {
            let leftButtonItem = editButtonItem()
            //leftButtonItem.tintColor = QorumColors.ThemeWhite
            navigationItem.leftBarButtonItem = leftButtonItem
        } else {
            navigationItem.leftBarButtonItem = nil
        }
    }

    public override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)

        if editing {
            navigationItem.leftBarButtonItem?.title = "Delete"
            //navigationItem.leftBarButtonItem?.tintColor = QorumColors.Nickname

            displaySelectionButtons = true
            title = "Delete Photos"

            let gridController = valueForKey("_gridController") as! MWGridViewController
            gridController.selectionMode = displaySelectionButtons
            gridController.collectionView!.reloadData()
        } else {
            let nav = self.navigationController as! TempPresentVC
            let photosToDelete = nav.selectedPhotos

            let afterButtonPress = {
                //self.navigationItem.leftBarButtonItem?.tintColor = QorumColors.ThemeWhite

                self.displaySelectionButtons = false
                self.updateNavigation()

                let gridController = self.valueForKey("_gridController") as! MWGridViewController
                gridController.selectionMode = self.displaySelectionButtons
                gridController.collectionView!.reloadData()
            }

            guard photosToDelete.count > 0 else {
                afterButtonPress()
                return
            }

            let title = "Delete Photo"
            let message = "Are you sure you want to delete these photos?"
            let action = "Delete"
            let cancelAction = "Cancel"

            let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
            let declineButton = UIAlertAction(title: cancelAction, style: .Default, handler: { (action: UIAlertAction) in
                afterButtonPress()
            })
            let acceptButton = UIAlertAction(title: action, style: .Default, handler: { (action: UIAlertAction) in
                afterButtonPress()
            })
            alert.addAction(declineButton)
            alert.addAction(acceptButton)
            UIApplication.topMostController().presentVC(alert) //private lib
        }
    }
}



class TempPresentVC: UINavigationController, MWPhotoBrowserDelegate {
    var selectedPhotos = [Int]()

    func photoBrowser(photoBrowser: MWPhotoBrowser!, photoAtIndex index: UInt, selectedChanged selected: Bool) {
        if selected {
            selectedPhotos.append(index.toInt)
        } else {
            selectedPhotos.removeObject(index.toInt)
        }
    }
}

这不允许您在单一照片查看模式下删除照片,但从此代码库开始,该部分很容易实现。

请注意,我在里面使用的一些东西是我在其他课程中写的私函数,但他们应该清楚的是。