class AlbumModel {
let name:String
let count:Int
let asset:NSMutableArray
init(name:String, count:Int, asset:NSMutableArray) {
self.name = name
self.count = count
self.asset = asset
}
}
class yourCustomCell: UITableViewCell {
//MARK:- Properties
@IBOutlet weak var collectionView: UICollectionView!
//MARK:- initialization methods
override func awakeFromNib() {
super.awakeFromNib()
// setupView()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
//MARK:- Setup collectionView datasource and delegate
func setCollectionViewDataSourceDelegate<T:UICollectionViewDataSource & UICollectionViewDelegate>(dataSourceDelegate: T, forRow row: Int) {
collectionView.delegate = dataSourceDelegate
collectionView.dataSource = dataSourceDelegate
collectionView.tag = row
collectionView.reloadData()
}
}
class ViewController: UIViewController {
var tablearray = NSMutableArray()
func getAssetThumbnail(asset: PHAsset) -> UIImage {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var thumbnail = UIImage()
option.isSynchronous = true
manager.requestImage(for: asset, targetSize: CGSize(width: 100, height: 100), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
thumbnail = result!
})
return thumbnail
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//print(UnladenSwallow.unknown)
let fetchOptions = PHFetchOptions()
let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: fetchOptions)
let topLevelfetchOptions = PHFetchOptions()
let topLevelUserCollections = PHCollectionList.fetchTopLevelUserCollections(with: topLevelfetchOptions)
let allAlbums = [topLevelUserCollections, smartAlbums]
var name = ""
smartAlbums.enumerateObjects({
if let collection = $0.0 as? PHAssetCollection{
name = collection.localizedTitle!
let image_arry = NSMutableArray()
let result = PHAsset.fetchAssets(in: collection, options: nil)
result.enumerateObjects({ (object, index, stop) -> Void in
let asset = object
image_arry.add(self.getAssetThumbnail(asset: asset))
})
let newAlbum = AlbumModel(name: name, count: collection.estimatedAssetCount, asset:image_arry)
self.tablearray.add(newAlbum)
}
})
topLevelUserCollections.enumerateObjects({
if let collection = $0.0 as? PHAssetCollection{
name = collection.localizedTitle!
let image_arry = NSMutableArray()
let result = PHAsset.fetchAssets(in: collection, options: nil)
result.enumerateObjects({ (object, index, stop) -> Void in
let asset = object
image_arry.add(self.getAssetThumbnail(asset: asset))
})
let newAlbum = AlbumModel(name: name, count: collection.estimatedAssetCount, asset:image_arry)
self.tablearray.add(newAlbum)
}
})
print(self.tablearray)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UITableViewDataSource,UITableViewDelegate{
func numberOfSections(in tableView: UITableView) -> Int{
return self.tablearray.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let album = self.tablearray[section] as! AlbumModel
return album.name
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! yourCustomCell
cell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.section)
return cell
}
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let album = self.tablearray[collectionView.tag] as! AlbumModel
print("count = \(album.asset.count)")
return album.asset.count;
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let album = self.tablearray[collectionView.tag] as! AlbumModel
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "col", for: indexPath)
let img = cell.viewWithTag(111) as! UIImageView
img.image = album.asset.object(at: indexPath.row) as? UIImage
return cell
}
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: IndexPath) {
print("get selected collectionview itemindex \(indexPath.row)")
}