0

I have an OS X app written in Swift and I am attempting to iterate through a directory and save each image into an array of NSImages so that I can display them in an NSImageView one at a time.

My code at the moment is below:

func LoadImages() {
    let fileManager = NSFileManager.defaultManager()
    let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath(strPhotoDirectory)!
    while let element = enumerator.nextObject() as? String {
        if element.hasSuffix("jpg") || element.hasSuffix("jpeg") {
            self.images.append(NSImage(contentsOfFile: element)!)
        }
    }

    self.imageCurrent.image = self.images[0]    // Sets my imageView to the first image
}

where strPhotoDirectory is equal to a file path.

At the moment, the debugger crashes on the last line there (setting the imageView to the first image) because the array is out of range, which I take it to mean that the images were never saved in the first place, so the array is empty.

I've tried poing the debugger console to find some of the values of my variables as it runs and have found that the element.hasSuffix("XXX") returns false for jpg, jpeg, JPG, JPEG. The image I am trying to save has the file name VH-YIZ VOZ B738 - YSSY - 26-07-2015 - 1 - Rotating on 34R and the extension .JPEG.

What am I doing wrong?

Thanks in advance.

Edit When I run the code, it reaches the line specified above and outputs this:

strPhotoDirectory = /Users/Matt/Downloads/Test/
Total Path = /Users/Matt/Downloads/Test/VH-YIZ VOZ B738 - YSSY - 10-04-2015 - 1 - On short final for 25.JPG

then:

fatal error: unexpectedly found nil while unwrapping an Optional value

Matt Kelly
  • 1,451
  • 2
  • 13
  • 30
  • enumerator.nextObject() will actually return the file name and not the complete path. you need to append it to the directory path, in this case strPhotoDirectory, and get the full path to the file before you can create the NSImage. HTH – Shripada Aug 03 '15 at 03:40
  • I amended my code to be `self.images.append(NSImage(contentsOfFile: "\(strPhotoDirectory)\(element)")!)` but I still get the same error. – Matt Kelly Aug 03 '15 at 05:31
  • What is the value of strPhotoDirectory?, also can you also paste the one of the image paths generated? – Shripada Aug 03 '15 at 06:49
  • I have amended the question to include the things you mentioned. Hope this helps, thanks for giving me a hand :) – Matt Kelly Aug 03 '15 at 07:23

1 Answers1

0

I got your code to work by making sure NSImage loaded from the full path

   func LoadImages() {
        //
        // make certain to append a slash (/) at the end!
        let strPhotoDirectory = "/Users/me/Pictures/" 

        //
        let fileManager = NSFileManager.defaultManager()
        let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath(strPhotoDirectory)!
        while let element = enumerator.nextObject() as? String {
            if element.hasSuffix("jpg") || element.hasSuffix("jpeg") {
                if let image = NSImage(contentsOfFile: strPhotoDirectory + element)
                {
                    self.images.append(image)
                }
            }
        }

        self.imageCurrent.image = self.images[0]    // Sets my imageView to the first image
    }
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215