iphone: relative paths to local file? - iphone

Using MontoTouch for .NET C# iPhone development. (though should not matter)
In the iPhoneSimulator, I use UIImage.FromFile (#"images/Bahai.png"); to get the Bahai.png from the images folder.
However, when I run it in debug mode on my iTouch, the function returns a null.
Only if I put the image file in the root does it work in the iTouch.
Is there a different relative path I need to use?
Ian

Not sure if this will help, but in objective-C, I would load an image from the app bundle like this:
UIImage *image = [UIImaged imageNamed:#"Bahai.png"];
For other types of files, I would get the absolute path like this:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Bahai" ofType:#"png"];

Related

How to access images in library project from main project in iphone?

I have created one library project in iphone and my images folder is present in my main project. How do i access that images?
You can access the app's resources using NSBundle's methods:
NSString *imageFilePath = [[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"png"];
UIImage *image = [UIImage imageWithContentsOfFile:imageFilePath];
You can access the resources from NSBundle class.
If your resources is in current application executable, at that time you need to use "[NSBundle mainBundle]".
If you have main bundles in your application, we have a property in NSBundle called "allbundles". It will returns an array of all the application’s non-framework bundles.So that, you can call your resources from any of the bundle.
I think this may useful for you.

Images not shown in my iphone web app

I'm creating a web app and have a problem. My images is shown in my Safari browser and in the Iphone simulator, but they are not shown on my Iphone.
Any ideas?
If HTML files and images are stored locally on device, you have to keep in mid that iOS filesystem is case sensitive. so iPhone.jpg and iphone.jpg can be two different files... make sure that all your tags and corresponding files have correct case...
Also when loading uiwebview and resources for it are in bundle folder you have to make sure that you set baseURL so that webkit can find relative linked files:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
this enables you to link relative to bundle:
<img src=iPhone.jpg">

Playing a video in ipad

I want to play a movie in my app and the video is stored in my ipad library.
Can anyone provide me the necessary guidance for it?
Get the MPMediaItem of the video you want using MPMediaQuery
Get the Asset URL like this:
NSURL *videoURL = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];
Instantiate an MPMoviePlayerViewController using videoURL as the content URL.
If the video is stored in your App's bundle then do this to get the URL:
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:#"nameoffile" withExtension:#"mp4"];
See a sample of a custom view/viewController combo in Pragmatic iPad Programming. Check the source code for chapter 8 (free download from that page). They use a video provided as a file inside the project. Btw, if the file in the video appears red in XCode, you'll have to remove it and re add it, I think the project definition is a bit screwed.

iPhone dev: how to load images named the same from different groups

I am working on an iPhone project, and have two themes defined under different directories, with most files share the same names (like background.png, image1.png, image2.png, etc). I have had them imported into xcode under different group names (group1, group2).
How do I tell UIImage to load an image from a given group, say, I want group1:background.png?
Thanks,
Tim
Try this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"image1" ofType:#"png" inDirectory:#"group1"];
[UIImage imageWithContentsOfFile: path];

Using UIImagePNGRepresentation

I'm trying to save a UIImageView image to a png file, and then use that file as a GLTexture.
I'm having trouble with this, when I try to run my code in the simulator, Xcode says it succeeded, but the simulator crashes with no error messages.
Here is my code:
NSString *dataFilePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Picture.png"];
NSData *imageData = UIImagePNGRepresentation(imageView.image);
[imageData writeToFile:dataFilePath atomically:YES];
// Load image into texture
loadTexture("Picture.png", &Input, &renderer);
Judging by your reply to Metalmi, it looks like you're not giving us the actual code you're compiling. If you're actually doing what it looks like you're doing, then you're trying to write data to your application's bundle, which is going to fail because you're not allowed to do that. Can you provide us with the exact code you're trying to execute?
Try this:
NSString *dataFilePath = [[NSBundle mainBundle] pathForResource:#"Picture" ofType:#"png"];