Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 728
Spiral1ng Avatar asked Jul 03 '17 03:07

Spiral1ng


People also ask

How do I import a picture into a swift iOS app?

If your Swift iOS app needs to import a picture from the user’s device, you’ve come to the right place today. Let’s learn how to use UIImagePickerController to let the user select a photo from their device to load into your app. This is going to be a very simple storyboard. It’s going to be a button, and a UIImageView (to show our loaded image).

How to fetch photos from cameraroll/photos in Swift 3?

You can use the Photos framework to fetch the photos from CameraRoll/Photos. Here is the version of Swift 3 code. Use this function to fetch the photos, somewhere in viewDidLoad or on your action wherever you want to fetch the photos.

How do I implement the uiimagepickercontroller?

The UIImagePickerController is a view controller that gets presented modally. When we select or cancel the picker, it runs the delegate, where we handle the case and dismiss the modal. Let’s implement the photo library first, then the delegates. Add the following code to the photoFromLibrary method:

How do I put a photo into an UIImageView?

We will put a photo into an UIImageView when we first use the image, and doing so on an iPad is easier than an iPhone, though it will work on both. Right-click and save the image below: Click on Assets.Xcassets and drag the pizza file into the assets folder. Go back to the story board and select the clip icon. You will find the pizza media there.


1 Answers

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.

like image 188
Rajan Maheshwari Avatar answered Oct 15 '22 06:10

Rajan Maheshwari