2

I am having a real hard time understanding how to embed many images and sounds in an iPhone app. When I say embed, I mean I want the images and sounds to be downloaded with the app, and some of those images will appear when the app is run. Other images are just saved in the app's disk space, so that when a user presses a button, the image or sound will be accessed and displayed or played.

I have read tutorials on using 'core data' and such, but that only seems like a way to create objects that can be used to reference such data items, or for storing small files as binary data. Can someone please tell me how in xcode I can specify a directory of images and .mp3s to be loaded onto a phone with my app, and how I can then call those media with code?

Some example code for the following would completely solve my problem:

An app is loaded onto a phone, and it contains 3 buttons. If the user presses button #1, a sound is heard. Button #2 plays a different sound, and button #3 changes a UIImageView to a different picture. (this isn't a homework assignment or something, but if I could code this example, I could do everything I need to in my app and understand the process)

Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
jake9115
  • 3,964
  • 12
  • 49
  • 78
  • Is there any reason that adding the images/sounds in your project and using [UIImage imageNamed:@"foo"] and [NSBundle pathForResource:ofType:] doesn't work? – BergQuester May 21 '13 at 01:45
  • This probably works just fine, I just need to understand what you mean. So, I should just make a folder within my project called media, and use the File -> Add file ability to add all my images and sounds, and then what? How do I programatically display one of these images or play a sound? (I don't undestand this: [NSBundle pathForResource:ofType:]) – jake9115 May 21 '13 at 01:50

2 Answers2

2

I mean I want the images and sounds to be downloaded with the app

Simply add the images and sounds to your project. Make sure you specify (in the Add Files to Project dialog) that they are to copied into the project and that they are to be part of the target. The result will be that the images and sounds will be built into your app (in what is called its bundle, in particular the main bundle - see the NSBundle docs). You can then fetch them out, as needed, in code. For example, to fetch an image, use imageNamed:. To refer to a sound, refer to the file as a resource within the main bundle.

Other images are just saved in the app's disk space

That is a completely different matter. You will have to get the images from somewhere while the app runs. There is no way to have the user download the app and the image end up in the app's disk space: the app must populate its disk space, in real time, as the app runs. At the time the user downloads the app, the associated disk space, outside the app bundle itself, will be empty.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for the conceptual clarification, I thought this must be the case, but got confused when learning about iPhone persistence. This really clears things up! – jake9115 May 21 '13 at 02:06
2

For images, see the responses to this question.

Sound is similar to images, but is loaded differently, this tutorial show how to load and play sounds.

For files that are saved after the app is installed, you will need to download and save those files in your code. For starters, see Apple's guide to the iOS file system

Community
  • 1
  • 1
BergQuester
  • 6,167
  • 27
  • 39
  • Fantastic links. I gave the other user the check mark because it was more helpful on a conceptual basis, but your answer will certainly aid in my app development too! – jake9115 May 21 '13 at 02:07