0

I have an issue with a OSX application. I want it to display an image, so I create an NSImageView object, and linked it to my view controller. I then ceate a function that should load an image from a selected folder. I get the image url correctly an dtry to load the image onto the NSImageView but that doesn't work (with no rpeorted error). The image is a tiff file, and the isValid variable return false it seems...

imageAve is the name of the NSImageView outlet : @IBOutlet weak var imageAve: NSImageView!

Here the code of the function in question :

func loadImage() { //load the tiff file 
    let image_url = String(describing: self.loadedtraces.tracefilesurls[self.indexSelected])
    let image_url_cut = String(image_url[image_url.index(image_url.startIndex, offsetBy: 7)...image_url.index(image_url.endIndex, offsetBy: -8)] + "_ave_merged.tif")
    if FileManager.default.fileExists(atPath: image_url_cut) { //test if the file exists and that works fine.

        let image_average = NSImage(byReferencingFile: image_url_cut)

        if (image_average?.isValid == false) {
            self.alert.informativeText = "The image is invalid."
            self.alert.alertStyle = .informational
            self.alert.icon = alert_info_logo
            self.alert.beginSheetModal(for: self.view.window!)
        } else {
            self.imageAve.needsDisplay = true
            self.imageAve.image = image_average
        }
    } else {
        self.alert.informativeText = "The average merge image file does not exist in the traces folder."
        self.alert.alertStyle = .informational
        self.alert.icon = alert_info_logo
        self.alert.beginSheetModal(for: self.view.window!)
    }
}

Thanks to everyone who might able to help !

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Remi
  • 11
  • 2
  • Instead of manually removing the scheme file:// from your fileURL absoluteString you should just use your url path property. – Leo Dabus Nov 26 '17 at 13:34
  • Note that it is Swift naming convention to use camelCase instead of snake_case – Leo Dabus Nov 26 '17 at 13:38
  • `loadedtraces.tracefilesurls[indexSelected].path` – Leo Dabus Nov 26 '17 at 13:40
  • And don’t use String describing method. You will probably never need to use it. – Leo Dabus Nov 26 '17 at 13:41
  • Regarding your issue just turn off App sandbox in your target capabilities – Leo Dabus Nov 26 '17 at 13:46
  • Btw to compose a new file url in your directory url you should use url method `appendingPathComponent("yourFileName.ext")` – Leo Dabus Nov 26 '17 at 13:56
  • 1
    Thanks for all these advices, I'll modify my code accordingly ! You're right it is a sandbox issue... thanks again !! – Remi Nov 26 '17 at 14:01
  • I did want to keep my app sandboxed. The way I got around the issue was by asking the user to choose a folder instead of specific files. The folder is then added to the sandbox by the sepcific instance of the application. – Remi Dec 06 '17 at 22:02

0 Answers0