i need to get the path of the app (bundlePath or bundleURL) without the app name
NSUrl *strUrl = [[NSBundle mainBundle]bundleURL];
NSString *str = [[NSBundel mainBundel]bundlePath];
how do i go about
Both NSURL and NSString have methods to remove the last path component, so this should give what you are looking for:
NSURL *strUrl = [[[NSBundle mainBundle] bundleURL] URLByDeletingLastPathComponent];
NSString *str = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
Related
I'm completely new to Xcode, obj-c etc, but I want to use Parse.com to save an image to their server, they say you have to use this line, to convert a UIImage to NSData
NSData *imageData = UIImagePNGRepresentation(image);
How would I load one of the .png's that is in my resource folder and then convert it to a UIImage? I'm also using the Sparrow Framework if that helps any.
EDIT
I've tried this code as suggested below.
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"terrainHexAtlas_v1#2x.png" ofType:#"png"];
UIImage *myImage = [UIImage imageWithContentsOfFile:imagePath];
NSData *imageData = UIImagePNGRepresentation(myImage);
PFFile *imageFile = [PFFile fileWithName:#"terrainHexAtlas_v1#2x.png" data:imageData];
[imageFile save];
But nothing seems to be happening. I tried a breakpoint at the last line and the vars imagePath, myImage, imageData are all 00000000 which I don't get.
you should follow code:
//png
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"your_image" ofType:#"png"];
UIImage *myImage = [UIImage imageWithContentsOfFile:imagePath];
NSData *pngImageData = UIImagePNGRepresentation(myImage);
//jpeg
NSString *imagePath2 = [[NSBundle mainBundle] pathForResource:#"your_image" ofType:#"jpg"];
UIImage *myImage2 = [UIImage imageWithContentsOfFile:imagePath2];
NSData *jpegImageData = [UIImageJPEGRepresentation(myImage2, 1.0f)];
Do not contain an extension in pathForResource. check following mistake case.
// not contain an extension
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"your_image.png" ofType:nil];
// contain an extension
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"your_image" ofType:#"png"];
// some people mistake case. wrong code. that return nil
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"your_image.png" ofType:#"png"];
I am trying to change desktop image using below code:
NSError *anError = nil;
NSString *imageURLString = [[NSBundle mainBundle] pathForResource:#"wallpaper" ofType:#"jpg"];
NSURL *imageURL = [NSURL URLWithString:imageURLString];
[[NSWorkspace sharedWorkspace] setDesktopImageURL:imageURL forScreen:[NSScreen mainScreen] options:nil error:&anError];
but it is always throwing this exception message in console:
Invalid parameter not satisfying: url != nil && [url isFileURL]
When I tried to log - [url isFileURL] it is returning NO, which I am unable to figure out.
When I tried to print description of imageURL, it printed following message:
<CFURL 0x100165c50 [0x7fff7f508ea0]>{type = 15, string = /Users/miraaj/Library/Developer/Xcode/DerivedData/DesktopFun-cevkcaebvcfnstgqbulqyibcfvru/Build/Products/Debug/DesktopFun.app/Contents/Resources/wallpaper.jpg, encoding = 134217984, base = (null)}
Can anyone suggest me the reason for this problem?
You're using the wrong method to create an NSURL. You want
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"wallpaper" ofType:#"jpg"];
NSURL* imageURL = [NSURL fileURLWithPath:imagePath isDirectory:NO];
Or, you could save a step and get an NSURL directly from NSBundle (on 10.6 and later):
NSURL* imageURL = [[NSBundle mainBundle] URLForResource:#"wallpaper" withExtension:#"jpg"];
I'm making an app where I need to copy certain file from the app bundle to the user's Application Support folder. This is the code I'm using actually:
NSBundle *thisBundle = [NSBundle mainBundle];
NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleExecutable"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *userpath = [paths objectAtIndex:0];
userpath = [userpath stringByAppendingPathComponent:executableName]; // The file will go in this directory
NSString *saveFilePath = [userpath stringByAppendingPathComponent:#"db.sqlite"];
NSFileManager *fileManager = [[NSFileManager alloc] init];
[fileManager copyItemAtPath:[thisBundle pathForResource:#"db" ofType:#"sqlite"] toPath:saveFilePath error:NULL];
But this doesn't copies anything, BTW this is a Mac app.
Finally got it working with adding
if ([fileManager fileExistsAtPath:userpath] == NO)
[fileManager createDirectoryAtPath:userpath withIntermediateDirectories:YES attributes:nil error:nil];
And the resulting code is:
NSBundle *thisBundle = [NSBundle mainBundle];
NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleExecutable"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *userpath = [paths objectAtIndex:0];
userpath = [userpath stringByAppendingPathComponent:executableName]; // The file will go in this directory
NSString *saveFilePath = [userpath stringByAppendingPathComponent:#"db.sqlite"];
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:userpath] == NO)
[fileManager createDirectoryAtPath:userpath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager copyItemAtPath:[thisBundle pathForResource:#"db" ofType:#"sqlite"] toPath:saveFilePath error:NULL];
You might want to double-check your file paths:
NSLog(#"src: %#", [thisBundle pathForResource:#"db" ofType:#"sqlite"]);
NSLog(#"dst: %#", saveFilePath);
When changing the image on an UIImageView in code, the image disappears. This only happens on an iPod Touch device, not on the iPhone emulator.
In my bundle I have an image named SomeImage.PNG
NSString *path = [[NSBundle mainBundle] pathForResource:#"SomeImage" ofType:#"png"];
[self.background setImage:[[UIImage alloc] initWithContentsOfFile:path]];
I must reiterate, this works fine on the iPhone simulator but not on an iPod Touch device.
Notice that the image file has an upper-case file extension, but in my code it is declared with lower case. This is the problem. So to fix the problem, change the code to this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"SomeImage" ofType:#"PNG"];
[self.background setImage:[[UIImage alloc] initWithContentsOfFile:path]];
I am asking if there is a 'better' way to do what I am doing, so I don't come across such issues in the future.
Not really. Unfortunately the iOS file system is case sensitive.
Something like this.....?
NSString *filename = #"SomeImage";
NSString *extension = #"png";
UIImage *img = nil;
NSString *path = nil;
path = [[NSBundle mainBundle] pathForResource:filename ofType:[extension lowercaseString]];
img = [[UIImage alloc] initWithContentsOfFile:path];
if (img == nil) {
path = [[NSBundle mainBundle] pathForResource:filename ofType:[extension uppercaseString]];
img = [[UIImage alloc] initWithContentsOfFile:path];
}
i have a plist placed inside en.lproj. I am trying to get its path this way,
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSMutableString *localizedPath = [[NSMutableString alloc] initWithFormat:#"%#/%#.%#/%#",bundlePath,lang,#"lproj",#"1.plist"];
where 1.plist is in en.lproj/1.plist put in resources.
Does [[NSBundle mainBundle] bundlePath]; give correct path to my resources? Can any body give me any clue. I am using this for iPhone , and i am new to Mac os x development, can it be used for mac os x development as well?
--
Regards,
U'suf
It get the path to the resources directory, use
NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
To get the path to a particular resource though use
NSString * resourcePath = [[NSBundle mainBundle] pathForResource:#"1" ofType:#"plist"];
The above will get the resource from the localization declared by the user in their general settings.
If you want to specify the localization yourself, then try
NSString * resourcePath = [[NSBundle mainBundle] pathForResource:#"1" ofType:#"plist" inDirectory:nil forLocalization:localization];
What you want is -pathForResource:ofType: