Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Ryan Maneo
Ryan Maneo
4,342 Points

How to add custom data? [Swift]

Hello there... I'm not sure exactly how to ask this... but when you add a "Button" for example, you drag and drop it onto the storyboard, then add the code manually or drag the code to the view controller. But lets say you don't want a button... lets say you want a spinning logo gif that is on the homepage of your app... how do I add custom data like that to my code? If that makes sense? How do I access external data other than the just drag n' drop data that is built into Xcode?

1 Answer

I'm not exactly sure what you mean but I'll try to answer in multiple ways. First the "spinning logo gif". Do you mean the activity indicator to show loading? If so, it's similar to a button - you can drop it onto a view controller of your choosing and connect it with an IBOutlet or programmatically create it.

If you mean assets, like a picture of your logo for instance, you can create them in the size needed (remember to take into account @2x and @3x sizes - if its a 100x100 size needed, you create it as 300x300 and I generally drop it into Asset Catalog Creator), copy them into your Assets folder and use them in your storyboards or programmatically with something like UIImage(named: "yourImageName"). If you wanted to pull a picture from a URL instead (let's say you're hosting your assets online in Cloudinary or AWS), you can direct a URL with something like this:

  let imageURL = NSURL(string: "http://www.yourURLString.com")
  downloadImage(imageURL!, imageView: yourImageView)

 func downloadImage(url: NSURL, imageView: UIImageView){
        getDataFromUrl(url) { (data, response, error)  in
            dispatch_async(dispatch_get_main_queue()) { () -> Void in
                guard let data = data where error == nil else { return }
                imageView.image = UIImage(data: data)
            }
        }
    }

Is that what you mean?

Ryan Maneo
Ryan Maneo
4,342 Points

I mean, yes... But generally, what's provided in Xcode at default isn't enough to build a game for example. Where do all of those files go? Be they gifs, images, videos, animations, etc. I know what to do from there, viewcontroller and connect / customize code, but it's the before part im confused by.