Swift Photo Library Access

2020-02-06 10:36发布

I would like to access photos from the user's photo library in my app and I was looking at the UIImagePickerController to do so. However, I was wondering if it is possible to access and view the photos from the photo library without actually storing those photos in the app? So basically the app would store references to the selected photos, rather than storing the photos themselves. This is to prevent the app from taking up large amounts of space from storing each photo.

Thanks!

1条回答
贪生不怕死
2楼-- · 2020-02-06 11:18

It is not so simple but as mentioned by Rob you can save the photo asset url and later fetch it using the Photos framework. You can fetch them using PHImageManager method requestImageData.

import UIKit
import Photos
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    @IBOutlet weak var imageView: UIImageView!
    let galleryPicker = UIImagePickerController()
    // create a method to fetch your photo asset and return an UIImage on completion
    func fetchImage(asset: PHAsset, completion: @escaping  (UIImage) -> ()) {
        let options = PHImageRequestOptions()
        options.version = .original
        PHImageManager.default().requestImageData(for: asset, options: options) {
            data, uti, orientation, info in
            guard let data = data, let image = UIImage(data: data) else { return }
            self.imageView.contentMode = .scaleAspectFit
            self.imageView.image = image
            print("image size:", image.size)
            completion(image)
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // lets add a selector to when the user taps the image
        let tap = UITapGestureRecognizer(target: self, action: #selector(openPicker))
        imageView.isUserInteractionEnabled = true
        imageView.addGestureRecognizer(tap)
    }
    // opens the image picker for photo library
    func openPicker() {
        galleryPicker.sourceType = .photoLibrary
        galleryPicker.delegate = self
        present(galleryPicker, animated: true)
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // check if there is an url saved in the user defaults
        // and fetch its first object (PHAsset)
        if let url = UserDefaults.standard.url(forKey: "assetURL"),
            let asset = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil).firstObject {
            fetchImage(asset: asset) { self.imageView.image = $0 }
        }

    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true)
        print("canceled")
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let url = info[UIImagePickerControllerReferenceURL] as? URL,
            let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            UserDefaults.standard.set(url, forKey: "assetURL")
            print("url saved")
            self.imageView.image = image
        }
        dismiss(animated: true)
    }
}

Note: Don't forget to edit your info plist and add "Privacy - Photo Library Usage Description"

Sample

查看更多
登录 后发表回答