美文网首页
iOS显示相薄

iOS显示相薄

作者: Harvace | 来源:发表于2016-11-04 21:47 被阅读26次

    显示手机中的相薄列表我自己封装了一个,Mark一下便于以后用。我的这个是Swift版本的,OC版本的移步官方例子

    效果如图
    
    
    import UIKit
    import AssetsLibrary
    
    class AlbumTableView:UITableView,UITableViewDelegate,UITableViewDataSource{
        
        var assetsLibrary:ALAssetsLibrary?
        var groups:NSMutableArray?
        
        typealias ClickCallback = (assetsGroup:ALAssetsGroup?)->Void
        var callback:ClickCallback?
        
        static let rowHeight: CGFloat = 88
        let mPhotoAlbumCellId = "mPhotoAlbumCellId"
        
        init() {
            super.init(frame: CGRect(x: 0, y: Margin.barHeight, width: Margin.screenW, height: Margin.screenH),style: .Plain)
            setupUI()
        }
        
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)!
        }
        
        func setupUI(){
            assetsLibrary = ALAssetsLibrary.init()
            groups = NSMutableArray()
            
            let groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos
            
            self.assetsLibrary?.enumerateGroupsWithTypes(groupTypes, usingBlock: { (group:ALAssetsGroup!, stop:UnsafeMutablePointer<ObjCBool>) in
                if group != nil{
                    let onlyPhotosFilter = ALAssetsFilter.allPhotos()
                    group.setAssetsFilter(onlyPhotosFilter)
                    if group.numberOfAssets()>0 {
                        self.groups?.addObject(group)
                        //self.reloadData()
                    }
                    else{
                        self.performSelectorOnMainThread(#selector(self.reloadData), withObject: nil, waitUntilDone: false)
                    }
                }
                }, failureBlock: { (err:NSError!) in
            })
            self.delegate = self
            self.dataSource = self
        }
        
        //MARK:TableView
        func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }
        
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.groups?.count ?? 0
        }
        
        func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
            return AlbumTableView.rowHeight
        }
        
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let i = groups![indexPath.row] as! ALAssetsGroup
            let cell = PhotoAlbumCell.init(style: .Default, reuseIdentifier: mPhotoAlbumCellId)
            let posterImage = UIImage(CGImage: i.posterImage().takeUnretainedValue())
            cell.previewIv.image = posterImage
            cell.titleLabel.text = i.valueForProperty(ALAssetsGroupPropertyName) as? String
            cell.subTitleLabel.text = "\(i.numberOfAssets())"
            return cell
        }
        
        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            if let a = self.indexPathForSelectedRow {
                self.deselectRowAtIndexPath(a, animated: false)
            }
            if let c = callback{
                c(assetsGroup: self.groups![indexPath.row] as? ALAssetsGroup)
            }
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:iOS显示相薄

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