0

I am trying to save an image which is taken from the camera into a separate album each time the image is taken. Now with the code i have written the image is saving but into the shared photo album, it is not saving into the folder i want it to be saved in but does create the folder. Also i want a new folder to be created each time the image is taken with the album name as 'LiQu' but i need it to have an ID number next to the folder which is taken from the previous view controller. I have uploaded my attempt at the code.

The view-controller where i am trying create a directory and save the image to.

import UIKit
import Photos

let albumName = "LiQu"  // "+ID" i want to add the ID number from the previous view controller which is entered by the user
var transferImage: UIImage?

//var redlineX: Double = 0


class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    var albumFound : Bool = false
    var assetCollection: PHAssetCollection!
    var photosAsset: PHFetchResult<AnyObject>!



    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.init(red: 0.32, green: 0.667, blue: 0.754, alpha: 1)

        //Check if LiQu album exists

        let fetchOptions = PHFetchOptions()

        fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)

        let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)


        if (collection.firstObject != nil){

        // found the album

        self.albumFound = true
        self.assetCollection = collection.firstObject!

        }else{
            // create the album


            PHPhotoLibrary.shared().performChanges({
                //let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: albumName)
                PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: albumName)
            })

                  }

    }

The view-controller from where i am wanting to use the "ID" entered as the name of the directory in the code above.

import UIKit

class FormViewController: UIViewController {

    @IBOutlet weak var ID: UITextField!

    @IBOutlet weak var organdonationno: UITextField!

    @IBOutlet weak var donorage: UITextField!

    @IBOutlet weak var snodname: UITextField!

    @IBOutlet weak var donorshospital: UITextField!

    @IBOutlet weak var date: UITextField!

I hope this makes sense and any help given is greatly appreciated, i am a newbie at this so please bear with me if i have made any mistakes.

ThankYou

Aneesa
  • 113
  • 3
  • 10

1 Answers1

0

You can pass you ID from FormViewController to ViewController and append id to your album name while creating album. Define id in ViewController as

var id : String = "" 

Pass id from FormViewController to your ViewController as

let myVC = storyboard?.instantiateViewControllerWithIdentifier("ViewController ") as! ViewController 
myVC.id= ID!
navigationController?.pushViewController(myVC, animated: true)

Append id to album name before creating new album to save your image as.

albumName += id.

You can refer this link for help saving image to custom album in PHAssest.

luckyShubhra
  • 2,606
  • 1
  • 9
  • 18