Accessing the image which is in external bundle - iphone

I created a bundle with name applausible.bundle, i placed the bundle in supporting files.This is my code
NSBundle* myBundle;
myBundle = [NSBundle bundleWithPath:#"/Library/applausible.bundle"];
NSString *path = [myBundle pathForResource:#"splash.png" ofType:nil];
[imageName setImage:[UIImage imageNamed:path]];
Here I am unable to display the image on the image view. Another way I tried like this I place the image on the image view using xib,while running the app. The output is coming like this in console
Could not load the "splash_screen high resolution.png" image referenced from a nib in the bundle with identifier "com.xxxxxxx.ImageSample". Can any one tell me where i made a mistake.Finally my goal is to display the image on the UIImageview which is in external bundle.

This may help you.
NSString bundlePath = [[NSBundle mainBundle] pathForResource:#"applausible" ofType:#"Bundle"];
NSLog(#"Bundle path is %#",bundlePath);
NSBundle myBundle;
myBundle = [NSBundle bundleWithPath:bundlePath];
NSString *path = [myBundle pathForResource:#"splash_screen high resolution" ofType:#"png"];
UIImage *image=[[UIImage alloc]initWithContentsOfFile:path];
[imageName setImage:image];
please try it out.

It may be helpful
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"applausible" ofType:#"Bundle"];
using this method you can get the bundle path.Then use your code from second line
myBundle = [NSBundle bundleWithPath:bundlePath];
NSString *path = [myBundle pathForResource:#"splash" ofType:n#"png"];
[imageName setImage:[UIImage imageNamed:path]];

Related

how to load image from subfolder of resources

hi i created a new group in my code and put the all images in that now resources\CountryFlags is the path i am doing this
NSString *fileName = countryInfo.ImageUrl;
CCSprite *flag ;
NSString * fullPath = [[NSBundle mainBundle] pathForResource: [fileName stringByDeletingPathExtension]
ofType: [fileName pathExtension]
inDirectory: #"CountryFlags"];
if (fullPath)
{
UIImage *theImage = [UIImage imageWithContentsOfFile: fullPath];
if (theImage)
{
flag = [CCSprite spriteWithCGImage: [theImage CGImage] key: fileName];
flag.position = ccp(200, 265);
flag.scale = .255;
}
}
but fullpath always got nil and not getting the code so any one have any idea how to solve this
I think it's because there is no "CountryFlags" folder in your app's bundle. XCode does not copy directory structure of resources unless folders are added as folder references (blue folder icons in project navigator, not yellow).

Reading Image from the Local Folder in Objective C

I need to Read a Image from the specific URL .
It works fine with WWW . but it returns a nil when the URL pointing the Local Folder .
// Works
NSString *sampleData = #"http://blogs-images.forbes.com/ericsavitz/files/2011/05/apple-logo2.jpg";
// Returns nil
NSString *sampleData = #"USER/user2/...";
Note :
I am changing the NSString to NSURL and creating the UIImage .
NSURL *url = [NSURL URLWithString: data];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
You are supplying a relative pathname for the file URL. That relative pathname is interpreted relative to the current working directory of the running application, which isn't guaranteed to be anything in particular, and so is almost certainly not what you want.
You can either supply an absolute path - one that starts with '/' - or set your app's current working directory to something explicit, like your user's Documents folder.
you probably should have a look into the NSBundle Class.
Methods like
- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)extension subdirectory:(NSString *)subpath
or
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension
is probably what you want
First of all, you can NOT read file from such path you given: "USER/user2/...", the file must in your App bundle or in your App's sandbox.
Second, check your path string if there was some texts need to be encoded in URL. Try:
NSURL *url = [NSURL URLWithString:[data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Also, if the url is not nil, you should also check if your [NSData dataWithContentsOfURL:url]; is returning nil. If so, it means your URL is not correct so the method cannot find your file.
P.S., You are mistyping your image create code, you should call alloc before imageWithData:.
You should do something like to get the local url :
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/%#", docDir, nameOfFile];
and finaly, load your image :
UIImage *image = [UIImage imageWithContentsOfFile:pngFilePath];
Try these instead
NSString *path = #"USER/user2/.../xxx.xxx";
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isFileExist = [fileManager fileExistsAtPath:path];
UIImage *image;
if (isFileExist) {
image = [[UIImage alloc] initWithContentsOfFile:path];
}
else {
// do something.<br>
}

How to create a NSMutableDictionary using dictionaryWithContentsOfFile: method

I want create and return a dictionary using the keys and values found in a file specified by a given path. I have my file on my Desktop:ciudades.txt (a human readable file!!! no a xml, just for practice). What method of my NSString i need to use and how? Please can somebody help me filling on my code the XXXXXXX. Thanks in advance
- (NSMutableDictionary)ciudades
{
if (!ciudades) {
NSString *path = [NSString XXXXXXXXX];
ciudades = [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
Define a function.
-(NSMutableDictionary*) ReadFileAsDictionaryForPath:(NSString* ) path
{
return [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
Use it as below
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
NSMutableDictionary* myDictionary = [self ReadFileAsDictionaryForPath:path];
if(!myDictionary)
{
NSLog(#"Error while reading data from file at path:%#",path);
}
First add that file to your application bundle By adding a existing file from xcode to your project. Then use this method to get the file path for example I'm getting a image's path.
NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:#"png"];
then try the dictionaryWithContentsOfFile method and see if it works or not.

How to access files in interior of an iPhone App?

I need in a iPhone app to access files that the app is build with(.plist etc). There's an hardcoded way to do this:
NSString *appDir = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
objectAtIndex:0]
stringByDeletingLastPathComponent]
stringByAppendingPathComponent:appFolder];
where appFolder is the name of folder app, like "test.app". After the appDir is known, to access files is simple.
Is there any other, not-hardcoded way to have access to files form the app?
Thanks in Advance!
NSString* pathToFile =
[[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"myFile.plist"];
// or, even better (handling localization):
NSString* pathToFile = [[NSBundle mainBundle] pathForResource:#"myFile"
ofType:#"plist"];
Your app folder is the "main bundle". So you can use NSBundle methods such as
NSString* path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"plist"];

pathForResource? without using extension (Iphone)

Here is what I'm doing, when I create an image with the path in the bundle:
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image" ofType:#"jpg"]];
What I want to do is trying to find the path for my image but without using the extension, without using 'ofType' (because the name of my image and her extension is store in my database) something like that:
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image.jpg"]];
But I don't know how to do it.
Best regards,
Why don't you split the string that you get from the DB?
NSString* fullFileName = #"image.jpg";
NSString* fileName = [[fullFileName lastPathComponent] stringByDeletingPathExtension];
NSString* extension = [fullFileName pathExtension];
Now you can simply use:
[[NSBundle mainBundle] pathForResource:fileName ofType:extension]];
You can easily use the NSBundle method without passing the extension, just pass nil for extension.
[[NSBundle mainBundle] pathForResource:#"image.jpg" ofType:nil];
- (NSData *)applicationDataFromFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
return myData;
}
taken from http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/FilesandNetworking/FilesandNetworking.html#//apple_ref/doc/uid/TP40007072-CH21-SW21
Could be easily adapted if you wanted it to return a UIImage instead of an NSData.
Also, you don't say if you are saving the images to the documents directory, or adding them to your app bundle before compiling. because if it's the latter, you can use [UIImage imageNamed:(NSString *)filename] to get the image. It expects an extension as part of the file-name.
The easiest way is to store the name and file type in your database separately, and retrieve them using the first method.I'm not sure that you can be successful in implementing the latter one.
I found that with an extension of ".jpg" it was necessary to use ofType for the extension for the app to work on an iPod Touch, whereas with an extension of ".png" I could just put "image.png" in pathForResource and say ofType:nil. But all versions worked on the simulator.
The app bundle contains the image file, and I am using:
[[NSBundle mainBundle] pathForResource:#"Auto" ofType:#"jpg"]
to get a path.