Swift照片库访问

5
我想在我的应用程序中访问用户相册中的照片,我正在考虑使用UIImagePickerController来实现。但是,我想知道是否有可能在不实际存储这些照片的情况下访问和查看照片库中的照片?因此,应用程序将仅存储对所选照片的引用,而不是存储照片本身。这是为了防止应用程序由于存储每个照片而占用大量空间。

谢谢!


2
是的,你可以在传递给你的字典中捕获与UIImagePickerControllerReferenceURL关键字相关联的值。然后,当你需要时,只需使用该URL从照片框架获取图像,但无需将图像本身保存在你的应用程序中。 - Rob
1个回答

14

虽然不是很简单,但正如Rob所提到的那样,您可以保存照片资源URL,稍后使用Photos框架获取它们。您可以使用PHImageManager方法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
    override func viewDidLoad() {
        super.viewDidLoad()
        // lets add a selector to when the user taps the image
        imageView.isUserInteractionEnabled = true
        imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(openPicker)))
        // request authorization
        switch PHPhotoLibrary.authorizationStatus() {
        case .authorized:
            print("The user has explicitly granted your app access to the photo library.")
            return
        case .denied:
            print("The user has explicitly denied your app access to the photo library.")
        case .notDetermined:
            PHPhotoLibrary.requestAuthorization { status in
                print("status", status)
            }
        case .restricted:
            print("Your app is not authorized to access the photo library, and the user cannot grant such permission.")
        default: break
        }
    }
    // opens the image picker for photo library
    @objc func openPicker(_ gesture: UITapGestureRecognizer) {
        galleryPicker.sourceType = .photoLibrary
        galleryPicker.delegate = self
        present(galleryPicker, animated: true)
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.imageView.contentMode = .scaleAspectFit
        // check if there is an url saved in the user defaults
        // and fetch its first object (PHAsset)
        if let assetURL = UserDefaults.standard.url(forKey: "assetURL") {
            if let asset = PHAsset.fetchAssets(withALAssetURLs: [assetURL], options: nil).firstObject {
                asset.asyncImageData { data, _, _, _ in
                    print("fetched")
                    guard let data = data else { return }
                    self.imageView.image = UIImage(data: data)
                }
            } else {
                print("assetURL:", assetURL)
                self.imageView.image = UIImage(contentsOfFile: assetURL.path)
            }
        } else {
            print("no assetURL found")
        }

    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true)
        print("canceled")
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        self.imageView.image = info[.originalImage] as? UIImage
        if let phAsset = info[.phAsset] as? PHAsset {
            phAsset.asyncURL { url in
                guard let url = url else { return }
                UserDefaults.standard.set(url, forKey: "assetURL")
                print("assetURL saved")
            }
        }
        dismiss(animated: true)
    }
}

extension PHAsset {
    func asyncURL(_ completion: @escaping ((URL?) -> Void)) {
        switch mediaType {
        case .image:
            let options: PHContentEditingInputRequestOptions = .init()
            options.canHandleAdjustmentData = { _ in true }
            requestContentEditingInput(with: options) { editingInput, _ in
                completion(editingInput?.fullSizeImageURL)
            }
        case .video:
            let options: PHVideoRequestOptions = .init()
            options.version = .original
            PHImageManager.default()
                .requestAVAsset(forVideo: self, options: options) { asset, _, _ in
                completion((asset as? AVURLAsset)?.url)
            }
        default:
            completion(nil)
        }
    }
    func asyncImageData(version: PHImageRequestOptionsVersion = .original, completion: @escaping  (Data?, String?, UIImage.Orientation, [AnyHashable : Any]?) -> ()) {
        let options = PHImageRequestOptions()
        options.version = version
        PHImageManager.default()
            .requestImageData(for: self, options: options, resultHandler: completion)
    }
}
extension URL {
    var phAsset: PHAsset? {
        PHAsset.fetchAssets(withALAssetURLs: [self], options: nil).firstObject
    }
}

注意:别忘了编辑您的info.plist并添加“隐私-照片库使用说明”

示例


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接