Quick access to the library library

I would like to access photos from the user photo library in my application, and I looked at the UIImagePickerController to do this. However, I was wondering if it is possible to browse and view photos from the photo library without saving these photos in the application? Thus, basically the application will store links to selected photos, and not store the photos themselves. This is to ensure that the application does not take up a large amount of storage space for each photo.

Thanks!

+1
ios swift swift3 photo
source share
1 answer

It's not that simple, but as Rob mentioned, you can save the URL of the photo resource and then retrieve it using the framework. You can get them using the requestImageData method of the PHImageManager method.

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: PHFetchOptions()).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. Do not forget to edit your information layer and add "Privacy - description of the use of the photo library"

Example

+9
source share

All Articles