Join Simon Allardice for an in-depth discussion in this video Recursion and Directories, part of Code Clinic: Swift.
- View Offline
- Welcome to Code Clinic, I'm Simon Allardice. Every month in Code Clinic a different problem is given to a group of lynda.com developer authors and we each solve it using our programming language of choice. For me, that's Swift. This is a way to see how different programmers approach real world problems, see the pros and cons of languages and technologies, and maybe find a few tips and tricks to add into your day to day coding. The problem will require us to dive into two areas, working with files and directories, and also accessing image metadata. I've already done a Code Clinic where we searched visual image data, but this one's different.
Here's the deal. I'm being given a folder of unsorted images, where that folder itself might contain any number of subfolders with images inside those. But these aren't organized in any way. I need to find all the images in all the folders and subfolders, extract any description or caption from the metadata contained in that image, and then use that caption to reorganize all the photos into an alphabetical folder structure with one level of folders based on the internal captions, or descriptions, contained in the images.
Image files often contain a lot of additional data. They're not just the bytes that describe the pixels and colors of the image, they also have extra metadata, information about the image, like what date was this image taken, what camera was used, does the image have a description. Even things like the focal length, the exposure time, and whether the flash was turned on are often automatically stored as metadata inside an image file. By using a Macintosh, you can see this metadata by opening the image in Preview, then going to the Tools and Inspector.
You'll see an information tab, which will contain that extra metadata. You'll see things, perhaps, like Caption, Provider, a Creator of this image, you'll see information about the Pixel Height and the Pixel Width, and you'll see a lot of different terms up here. Sometimes things like IPTC, you'll see JFIF, you'll see EXIF. They are different ways to store image metadata. Sometimes you'll see one, sometimes you'll see the other, sometimes you'll see both. For example, this one has different set of information about it.
Unfortunately, the metadata isn't always the same, and there's no guarantee that an image will contain the metadata you want, or even any significant metadata at all. It depends on things like the camera that you used or if anybody has edited the image in a software application. But you will often see things like the caption, the dimensions, the camera type, the Color Space, even Exposure information, other details. Many are automatically added by the camera, and cell phones can also embed geographic location metadata, identifying longitude and latitude, and other things can be edited manually.
So to get to this result, we'll need to be comfortable with finding files at multiple levels, with moving things around the file system, with creating new directories in code, and of course, how to actually get to that image metadata. And that's not as straightforward as you might hope. So consider for a moment how you would approach this problem, and in the next video, I'll show you my solution.
Simon introduces challenges and then provides an overview of his solutions in Swift. Challenges include topics such as statistical analysis, searching directories for images, and accessing peripheral devices.