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

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];

Related

import *.pdf into my app iphone

how to import *.pdf all pdf from my iphone to my app like this add music file. http://developer.apple.com/library/ios/#samplecode/AddMusic/Introduction/Intro.html .Thanks in advance .New to iphone dev.Really appreciate any help.
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
NSFileManager *mgr = [[NSFileManager alloc] init];
NSArray *allFiles = [mgr contentsOfDirectoryAtPath:bundlePath error:NULL];
for (NSString *fileName in allFiles)
{
if ([[fileName pathExtension] isEqualToString:#"pdf"])
{
NSString *fullFilePath = [bundlePath stringByAppendingPathComponent:fileName];
// fullFilePath now contains the path to your pdf file
// DoSomethingWithFile(fullFilePath);
NSLog(#"file: %#",fullFilePath);
}
}
NSURL *url = [[NSBundle mainBundle] URLForResource:#"" withExtension: #"pdf"];
NSLog(#"File: %#",url);
You can just email the pdf file as attachment and the iphone can read it on its own. That feature already exists within the iOS of the iPhone or you could upload them somewhere (on your server) and send the links.
You can't just browse all the files on the phone from within your App.
As I said to you in the comment on your other questions (which, by the way is almost exactly the same as this one, but in the other question you ask about csv files rather than pdf), you need to read up about the App Sandbox.
In a nutshell, Apps can only see their own files and those that are written by the App. All apps have their own storage space and they can only see in that area.
Having said that, it is possible to pass some files around between apps, but they have to be written to support that. There is no such concept as a global file system on iOS devices.
READ THIS

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">

iPhone: How to download a full website?

what approach do you recommend me for downloading a website (one HTML site with all included images) to the iPhone?
The question is how to crawl all those tiny bits (Javascripts, images, CSS) and save them locally. It's not about the concrete implementation (I know how to use NSURLRequest and stuff. I'm looking for a crawl/spider approach).
Jail breaks won't work, since it is intended for an official (App Store) app.
Regards,
Stefan
Downloading? Or getting the HTML-source of the site and displaying it with a UIWebView?
If last, you could simply do this:
NSString *data = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://apple.com"] encoding:NSUTF8StringEncoding error:NULL];
// Load UIWebView with data
[webView loadHTMLString:data baseURL:[NSURL URLWithString:#"http://apple.com"]];
EDIT:
For this approach, you would probably be best off using a regex-library for iPhone to parse through the string and find needed objects.
You could use this: RegexKitLite, and do a couple of Regex-expressions to find, for example, <link rel="%" href="*"> and src="*". But you have to remember to store them and replacing the values of * with the new path.
Storing files:
You will get url's back from the regex-methods, and you can write the files from the url's like this:
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString pathToCurrentSite = [rootPath stringByAppendingPathComponent:[NSString stringWithFormat:#"/%#/", fullUrlToPage]];
for (urlString in urlStrings) {
NSData *stringData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
[fileManager createFileAtPath:[pathToCurrentSite stringByAppendingPathComponent:urlString] contents:stringData attributes:nil];
}
NSString *data;
NSData *pageData = [data dataUsingEncoding:NSASCIIStringEncoding];
[fileManager createFileAtPath:[pathToCurrentSite stringByAppendingPathComponent:#"index"] contents:pageData attributes:nil];
[fileManager release];
Install wget on your jail broken iPhone
Use the spanning hosts options to download everything from the site.
wget -rH -Dserver.com http://www.server.com/
But why do you want to do this on a mobile device? This is somthing that should be done on a real computer with lots of memory, disk space, bandwidth and multiple CPU cores.
Was looking for similar functionality and found this. Can't claim any credit for it, just wanted to make sure it was mentioned (as a drop-in solution) for people interested in it.
http://robnapier.net/offline-uiwebview-nsurlprotocol
You can't save websites to your phone, only view them (unless your jailbroken.)
Hope this clears up your confusion,
Lee.
Here is the Appstore link https://itunes.apple.com/us/app/sitesucker/id346896838?mt=8
The app downloads entire websites natively to the phone.

iphone: relative paths to local file?

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"];