Swift 3: Load photos from Photos/Camera Roll without using UIImagePickerController

Spiral1ng picture Spiral1ng · Jul 3, 2017 · Viewed 7.4k times · Source

This question has been asked before but there were no answers for Swift 3. I am looking for the same solution which I am stuck for the past 3 weeks.

I have done my research and watched numerous Youtube videos about loading images from Photos/Camera Roll into the app using UIImagePickerController but I want to access the photos without user actions.

I want to read a series of photos from camera roll and put them in a photo slide to show them one by one. How can I access these photos without UIImagePickerController?

Answer

Rajan Maheshwari picture Rajan Maheshwari · Jul 3, 2017

You can use the Photos framework to fetch the photos from CameraRoll/Photos.

Here is the version of Swift 3 code.

Import the photos framework

import Photos

//Array of PHAsset type for storing photos
var images = [PHAsset]()

Use this function to fetch the photos, somewhere in viewDidLoad or on your action wherever you want to fetch the photos.

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() 

    // To show photos, I have taken a UICollectionView       
    self.photosCollectionView.reloadData()
}

Rest are UICollectionView Datasource and Delegates. See the cellForItem datasource method on How to show the image.

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return images.count
    }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCollectionViewCell", for: indexPath) as! PhotoCollectionViewCell
    let asset = images[indexPath.row]
    let manager = PHImageManager.default()
    if cell.tag != 0 {
            manager.cancelImageRequest(PHImageRequestID(cell.tag))
        }
    cell.tag = Int(manager.requestImage(for: asset,
                                            targetSize: CGSize(width: 120.0, height: 120.0),
                                            contentMode: .aspectFill,
                                            options: nil) { (result, _) in
                                                cell.photoImageView?.image = result
        })
    return cell
}

Adjust the below delegates as per your need.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = self.view.frame.width * 0.32
    let height = self.view.frame.height * 0.179910045
    return CGSize(width: width, height: height)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 2.5
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

Make sure to keep the photos permission ON. If you click Don't Allow, then you have to manage the authorization too using PHPhotoLibrary.authorizationStatus()

You can read more about Photos framework.