Swift Photo Library Access [英] Swift Photo Library Access
问题描述
我想在我的应用中访问用户照片库中的照片,我正在查看UIImagePickerController。但是,我想知道是否可以访问和查看照片库中的照片而不实际将这些照片存储在应用程序中?所以基本上应用程序会存储对所选照片的引用,而不是存储照片本身。这是为了防止应用程序占用大量空间来存储每张照片。
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.
谢谢!
推荐答案
这不是那么简单但是Rob提到你可以保存照片资产网址,然后使用照片框架获取它。您可以使用PHImageManager方法requestImageData获取它们。
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)
}
}
注意:Don'忘记编辑你的信息plist并添加隐私 - 照片库使用说明
Note: Don't forget to edit your info plist and add "Privacy - Photo Library Usage Description"
这篇关于Swift Photo Library Access的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!