Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a reference to a camera roll image - Swift 4

Tags:

ios

swift

I have a really simple request that is apparently beyond me. All I want to do is allow the user to select an image from the camera roll and then store a ‘reference’ to that image in my app. I can then load the image from the camera roll when I need it.

I do not want to copy the image and save it elsewhere because I feel that it would be wasting space on the phone. I realise that the user could delete the image from their camera roll, but in the case of this app it does not matter. I will simply check for nil just before displaying the image. It will not affect the app functionality.

I can present the ImagePickerController and I am happily getting a UIImage by means of:

info[UIImagePickerControllerOriginalImage]

What I would like to do is use:

info[UIImagePickerControllerReferenceURL]

However, this is being removed and is no longer supported. I have therefore lost several hours of my life looking into PHAsset. This seems fine for retrieval, providing I have something to search for. I cannot seem to write a simple app that gets an image via the UIImagePickerController and then allows me to save a reference/URL/uniqueID etc. that I can then use via some other method to get the image back again.

I am more than happy to be laughed at, providing the person laughing shows me how silly I have been…

All help greatly appreciated.

like image 771
Simon Ireson Avatar asked Sep 06 '25 03:09

Simon Ireson


1 Answers

I needed to check the PHPhotoLibrary.authorizationStatus() programatically as the app was silently failing this. Adding the following to AppDelegate.swift resolved the issue (you need to import 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)
}
like image 120
Simon Ireson Avatar answered Sep 07 '25 19:09

Simon Ireson