Ad

Swift - Reach Personal Documents

- 1 answer

I research some topics about IOS personal data protection for App presentation. I can reach the document directory and upload all files from App to Server( or can collect all contact list). Is this avaliable at Photo Gallery?.

Ad

Answer

Yes, you can do that, pulling up photos from photolibrary using Photos.

Below is the example:

import Photos

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
//Array of PHAsset type for storing photos
var images = [PHAsset]()
override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self
    getImages()
    // Do any additional setup after loading the view, typically from a nib.
}

func getImages() {
    let assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)
    assets.enumerateObjects({ (object, count, stop) in
        // self.cameraAssets.add(object)
        self.images.append(object)
    })

    //In order to get latest image first, we just reverse the array
    self.images.reverse()

    self.tableView.reloadData()
}
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    let imageAsset = self.images[indexPath.row]
    PHImageManager.default().requestImage(for: imageAsset, targetSize: CGSize(width: 120.0, height: 120.0), contentMode: .aspectFill, options: nil) { (result, _) in
        cell?.imageView?.image = result
    }
    return cell!
    }

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return images.count
    }
    }
Ad
source: stackoverflow.com
Ad