存储相机胶卷图像的引用 - Swift 4

3
我有一个非常简单的请求,但显然对我来说超出了范围。我想要做的就是允许用户从相机胶卷中选择一张图片,然后在我的应用程序中存储对该图片的“引用”。当需要时,我可以从相机胶卷中加载图像。
我不想复制并保存图像,因为我觉得这将浪费手机上的空间。我意识到用户可能会从相机胶卷中删除图像,但在这个应用程序的情况下无所谓。在显示图像之前,我将只检查是否为空。这不会影响应用程序的功能。
我可以呈现ImagePickerController,并通过以下方式愉快地获得UIImage:
info[UIImagePickerControllerOriginalImage]

我希望做的是使用:

info[UIImagePickerControllerReferenceURL]

然而,这已被删除,不再支持。因此,我花费了数小时的时间研究PHAsset。对于检索来说,这似乎是可以接受的,只要我有东西可以搜索。但我似乎无法编写一个简单的应用程序,通过UIImagePickerController获取图像,然后允许我保存引用/URL/唯一标识等,以便我可以通过其他方法再次获取图像。
我非常乐意被嘲笑,只要那个人能够向我展示我有多愚蠢...
非常感谢所有的帮助。

1
我目前无法测试它,但根据文档,info[UIImagePickerControllerPHAsset]是一个 PHAsset ,它具有可以持久标识该对象的 localIdentifier - Martin R
你好,Martin。非常感谢这个。我尝试了那个,但是收到了一个空对象。我会坚持下去,看看是否解决了问题。 - Simon Ireson
结果证明,我需要添加一些代码来检查用户对照片库的授权。默默地,应用程序无法访问。一旦完成这个操作,info [UIImagePickerControllerPHAsset] 返回值。 - Simon Ireson
1个回答

2

我需要编写代码来检查PHPhotoLibrary.authorizationStatus(),因为应用程序在这方面默默失败。将以下内容添加到AppDelegate.swift即可解决此问题(您需要导入Photos):

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    photoLibraryAvailabilityCheck()

}

//MARK:- PHOTO LIBRARY ACCESS CHECK
func photoLibraryAvailabilityCheck()
{
    if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized
    {

    }
    else
    {
        PHPhotoLibrary.requestAuthorization(requestAuthorizationHandler)
    }
}
func requestAuthorizationHandler(status: PHAuthorizationStatus)
{
    if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized
    {

    }
    else
    {
        alertToEncouragePhotoLibraryAccessWhenApplicationStarts()
    }
}

//MARK:- CAMERA & GALLERY NOT ALLOWING ACCESS - ALERT
func alertToEncourageCameraAccessWhenApplicationStarts()
{
    //Camera not available - Alert
    let internetUnavailableAlertController = UIAlertController (title: "Camera Unavailable", message: "Please check to see if it is disconnected or in use by another application", preferredStyle: .alert)

    let settingsAction = UIAlertAction(title: "Settings", style: .destructive) { (_) -> Void in
        let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString)
        if let url = settingsUrl {
            DispatchQueue.main.async {
                UIApplication.shared.open(url as URL, options: [:], completionHandler: nil) //(url as URL)
            }

        }
    }
    let cancelAction = UIAlertAction(title: "Okay", style: .default, handler: nil)
    internetUnavailableAlertController .addAction(settingsAction)
    internetUnavailableAlertController .addAction(cancelAction)
    self.window?.rootViewController!.present(internetUnavailableAlertController , animated: true, completion: nil)
}

func alertToEncouragePhotoLibraryAccessWhenApplicationStarts()
{
    //Photo Library not available - Alert
    let cameraUnavailableAlertController = UIAlertController (title: "Photo Library Unavailable", message: "Please check to see if device settings doesn't allow photo library access", preferredStyle: .alert)

    let settingsAction = UIAlertAction(title: "Settings", style: .destructive) { (_) -> Void in
        let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString)
        if let url = settingsUrl {
            UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
        }
    }
    let cancelAction = UIAlertAction(title: "Okay", style: .default, handler: nil)
    cameraUnavailableAlertController .addAction(settingsAction)
    cameraUnavailableAlertController .addAction(cancelAction)
    self.window?.rootViewController!.present(cameraUnavailableAlertController , animated: true, completion: nil)
}

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