UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 77: Not working on real device

Forums > 100 Days of SwiftUI

@leogc  

I was able to complete the challenge, save and load images from the documents directory, all works well in the simulator. But when I try to run on real iphone the image save fails. Do you know if I need to enable this functionality somewhere or am I missing something?

2      

@Bnerd  

I guess you have enabled in Target the: Privacy - Photo Library Additions Usage Description, correct?

3      

As @Bnerd has mentioned that can clearly be a reason. I think I once failed with that inattentively. Check it

3      

@leogc  

Thanks for the feedback. The app access the photo library OK. I seem to be haveing an issue when saving to the image to the documentsdirectory.

    func saveImage(fileName: String) {
        let path = FileManager.documentsDirectory.appendingPathExtension(fileName)

        if let jpegData = yourUIImage.jpegData(compressionQuality: 0.8) {
            do {
                try jpegData.write(to: path, options: [.atomic, .completeFileProtection])
            }
                catch {
                    print("image \(fileName) failed to save")
                }
            }
    }

Again, works on the simulator, but not in the device giving me the catch print message.

2      

I think you should be using FileManager.documentsDirectory.appendingPathComponent(fileName) there instead of .appendingPathExtension(fileName). That should be used if you want to add a file extension like "txt", "pdf", "html", or "json" to the end of the file name, not the file name itself. It will place the "." in your string before whatever you give it as a parameter automatically. So that will mess up your file name by making it a hidden file.

3      

@leogc  

Thank you @Fly0strich for your answer. That was my problem and now works well!

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.