-2

I'm creating a wallpaper app for iOS. I've created a UIImageView, but am stuck on saving the image. I have solved the permissions but am unable to have the user save the image. I created the save button itself, but I don't know to make save any image from the image array in the user's image gallery.

Here is my code so far:

class ViewController: UIViewController {

    @IBOutlet var imageview: [UIScrollView]!

    @IBOutlet weak var saveButton: UIButton!

    @IBAction func saveButtonPressed(_ sender: UIButton) {
      // TODO: - How to save the image here 
    }

    let scrollView: UIScrollView = {
        let scroll = UIScrollView()
        scroll.isPagingEnabled = true
        scroll.showsVerticalScrollIndicator = false
        scroll.showsHorizontalScrollIndicator = false
        scroll.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        return scroll
    }()

    var imageArray = [UIImage]()

    func setupImages(_ images: [UIImage]){
        for i in 0..<images.count {
            let imageView = UIImageView()
            imageView.image = images[i]
            let xPosition = UIScreen.main.bounds.width * CGFloat(i)
            imageView.frame = CGRect(x: xPosition, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
            imageView.contentMode = .scaleAspectFit

            scrollView.contentSize.width = scrollView.frame.width * CGFloat(i + 1)
            scrollView.addSubview(imageView)
            //scrollView.delegate = (self as! UIScrollViewDelegate)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.addSubview(scrollView)

        imageArray = [#imageLiteral(resourceName: "1"),#imageLiteral(resourceName: "10"),#imageLiteral(resourceName: "9"),#imageLiteral(resourceName: "8"),#imageLiteral(resourceName: "3")]

        setupImages(imageArray)
    }
}
Wyetro
  • 8,439
  • 9
  • 46
  • 64
kaysoli
  • 9
  • 2
  • sorry, I am new here !! – kaysoli Feb 13 '20 at 17:36
  • yes, its about using UIImageWriteToSavedPhotosAlbum but I just want someone to tell me about to make the save button when tapped save the current image after scrolling? what code should be written true to make that button tappable and giving save actions, btw I couldn't find similar articles helpful... – kaysoli Feb 13 '20 at 18:59
  • i have copied all my code so you can see it all – kaysoli Feb 13 '20 at 20:48
  • Are you open to using Firebase to save this image? If so, I have plenty of code for that. :) – Kasey Feb 13 '20 at 20:57
  • @kaysoli if you have a new question do not edit your current question and code — ask a new question. If the `UIButton` isn't appearing, it is not related to the first issue of saving the image. Most likely your IBOutlet isn't actually hooked up to anything. – Wyetro Feb 14 '20 at 19:39

1 Answers1

1

You will need to add a saveImage function:

func saveImage(image: UIImage) -> Bool {
    guard let data = UIImageJPEGRepresentation(image, 1) ?? UIImagePNGRepresentation(image) else {
        return false
    }

    guard let directory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) as NSURL else {
        return false
    }

    do {
        try data.write(to: directory.appendingPathComponent("fileName.png")!)
        return true
    } catch {   
        print(error.localizedDescription)
        return false
    }
}

And then in saveButtonPressed:

let success = saveImage(image: imageArray[0])
print("Did \(success ? "" : "not ")store image successfully")

You'll need to add some logic to actually select the image.

Wyetro
  • 8,439
  • 9
  • 46
  • 64