1

In Android we get camera images only i.e. when we open gallery programmatically, but in iOS when we open Photos, we get camera roll images and other unwanted images along with it like whatsapp images. How to filter for only camera images when opening gallery?

What I have tried in Swift 4 is shown below:

func getPhotosAndVideos()
{
    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate",ascending: false)]

    fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)

    print("fetchOptions : ", fetchOptions)

    let assets = PHAsset.fetchAssets(with: fetchOptions)

    var results = NSMutableArray()
    assets.enumerateObjects { (obj, idx, bool) -> Void in

        results.add(obj)
    }

    let cameraRollAssets = results.filtered(using: NSPredicate(format: "sourceType == %@", argumentArray: [3]))
    results = NSMutableArray(array: cameraRollAssets)

    print("cameraRollAssets : ", cameraRollAssets)

    print("results : ", results)
}

I want just camera images taken by my phone.

Cœur
  • 37,241
  • 25
  • 195
  • 267
devNewbie
  • 47
  • 1
  • 5
  • Hello @devNewbie, there lot's of answer in stack-overflow for this question. you should search first after add question. – Yogesh Patel Jun 24 '19 at 13:23

1 Answers1

2

Hey you can do that using apple's default UIImagePickerController()

class testViewController: UIViewController {
    let picker = UIImagePickerController()
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    @IBAction openPicker(_ sender: UIButton) {
      self.picker.sourceType = .savedPhotosAlbum
      self.picker.delegate = self
      self.present(self.picker, animated: true, completion: nil)
    }
}

extension testViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        print(info)
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571