how to fetch all video folder from camera roll and disply them tableview swift ios

0

i want make a simple video player ...and i want get all folder of videos from camera roll and display them tableView and when click on tableview cell fetch all videos of selected folder

but i dont know how to fetch all videos album folder from camera roll..so please suggest me

thx in advance

i was try with this code but its dose not work proper so please suggest me what should i do next

import UIKit import Photos

class VideoAlbumViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

var arrOfTBLData : NSMutableArray = NSMutableArray()
var arrOfTBLDataAll : NSMutableArray = NSMutableArray()
var arrAssetCollection : NSMutableArray = NSMutableArray()

@IBOutlet weak var tblView: UITableView!

override func viewDidLoad()
{
    super.viewDidLoad()
    self.tblView.delegate = self
    self.tblView.dataSource = self
    getPhotoLibrary()
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrOfTBLData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "VideoAlbumViewCell", for: indexPath) as! VideoAlbumViewCell
   // cell.imgView.image = lastImg[indexPath.row]
    cell.lbltitle.text = ((self.arrOfTBLData.object(at: indexPath.row) as! NSDictionary).object(forKey: "title") as! String)
    cell.lblVideos.text = "\(((self.arrOfTBLData.object(at: indexPath.row) as! NSDictionary).object(forKey: "videoCount")) as! NSNumber)"

    return cell
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("select item")
    let Obj : AlbumwiseViewController = self.storyboard?.instantiateViewController(withIdentifier: "AlbumwiseViewController") as! AlbumwiseViewController
    Obj.dicAssetsVidio = NSMutableDictionary.init(dictionary: self.arrOfTBLData.object(at: indexPath.row) as! NSDictionary)
    self.navigationController?.pushViewController(Obj, animated: true)
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 200
}

//MARK:- Set Method
func getPhotoLibrary()
{
    self.arrOfTBLData = NSMutableArray()
    self.arrOfTBLDataAll = NSMutableArray()
    // Get PhotoLibrary
    DispatchQueue.global().async {
        let status = PHPhotoLibrary.authorizationStatus()
        if (status == PHAuthorizationStatus.authorized){
            self.getVideoFolderListFromGallery()
        }else {

            PHPhotoLibrary.requestAuthorization(({ (newStatus) in
                if (newStatus == PHAuthorizationStatus.authorized)
                {
                    self.getVideoFolderListFromGallery()
                }else
                {
                    // self.getPhotoLibrary()
                }
            }))

        }
    }
}

func getVideoFolderListFromGallery()
{
    self.arrAssetCollection = NSMutableArray()
    self.arrOfTBLDataAll = NSMutableArray()
    self.arrOfTBLData = NSMutableArray()
    DispatchQueue.global(qos: .default).async {
        let fetchFolder:PHFetchOptions? = PHFetchOptions()
        fetchFolder?.sortDescriptors = [NSSortDescriptor(key:"localizedTitle", ascending: true)]
        let fetchVideos: PHFetchOptions = PHFetchOptions()
        fetchVideos.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
        fetchVideos.predicate = NSPredicate(format: "mediaType = %d ", PHAssetMediaType.video.rawValue)
        let assetCollections1 = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: nil)

        for i in 0 ..< assetCollections1.count
        {
            if let assetCollection = assetCollections1[i] as? PHAssetCollection
            {
                self.arrAssetCollection.add(assetCollection)
            }
        }
        let assetCollections2 = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)
        for j in 0 ..< assetCollections2.count
        {
            if let assetCollection = assetCollections2[j] as? PHAssetCollection
            {
                self.arrAssetCollection.add(assetCollection)
            }
            if j == assetCollections2.count-1
            {
                for i in 0 ..< self.arrAssetCollection.count
                {
                    let assetCollectionItem : PHAssetCollection = self.arrAssetCollection.object(at: i) as! PHAssetCollection


                    let dic: NSMutableDictionary = NSMutableDictionary()
                    dic.setValue(assetCollectionItem.localizedTitle, forKey: "title")

                    let  assets = PHAsset.fetchAssets(in: assetCollectionItem, options: fetchVideos)
                    dic.setValue(assets.count, forKey: "videoCount")
                    dic.setValue(assets, forKey: "assets")

                    if assets.count > 0
                    {
                        self.arrOfTBLDataAll.add(dic)
                    }
                }
            }
        }

        DispatchQueue.main.async {
            self.arrOfTBLData = self.arrOfTBLDataAll
            self.tblView.reloadData()
        }
    }
}

}

Cute Love

Posted 2019-10-12T05:00:37.320

Reputation: 1

No answers