美文网首页iOS DeveloperiOS相关
Swift PhotoKit 相册多选

Swift PhotoKit 相册多选

作者: 江水东流 | 来源:发表于2017-05-08 17:51 被阅读662次

    现在网上方便用的swift相册多选几乎没有,于是自己写了一个,用的是PhotoKit,效果图如下
    demo github 下载地址

    相册多选.gif

    具体看代码就行,写这个相册多选以及图片浏览器有很多细节需要注意的,我也用了不少心血.想了解的看代码.我就说说用法

        let album = JDAlbumGroupController()
    
        album.selectImgsClosure1 = { (assets: [PHAsset]) in
            for index in 0..<assets.count {
                let imgVi = UIImageView()
                imgVi.jiuFrame(index: index, column: 3, viW: 100, viH: 100, topMargin: 10)
                self.view.addSubview(imgVi)
                self.getLitImage(asset: assets[index], callback: { (image) in
                    imgVi.image = image
                })
            }
        }
        let nav = UINavigationController(rootViewController: album)
        self.present(nav, animated: true, completion: nil)
    

    typealias ImgCallBackType = (UIImage?)->()

    //获取缩略图 缩略图大小需要自己指定
    private func getLitImage(asset: PHAsset,callback: @escaping ImgCallBackType){
        PHImageManager.default().requestImage(for: asset,
                                              targetSize: CGSize(width: 100, height: 100) , contentMode: .aspectFill,
                                              options: nil, resultHandler: {
                                                (image, _: [AnyHashable : Any]?) in
                                                if image != nil{
                                                    callback(image)
                                                }
                                                
        })
        
    }
    
    
    //获取原图
    private func getOrangeImage(asset: PHAsset,callback: @escaping ImgCallBackType){
        //获取原图
        PHImageManager.default().requestImage(for: asset,
                                              targetSize: PHImageManagerMaximumSize , contentMode: .aspectFill,
                                              options: nil, resultHandler: {
                                                (image, _: [AnyHashable : Any]?) in
                                                if image != nil{
                                                    callback(image)
                                                }
                                                
        })
        
    }

    相关文章

      网友评论

        本文标题:Swift PhotoKit 相册多选

        本文链接:https://www.haomeiwen.com/subject/cbqvtxtx.html