Unable to change desktop image because url is not file url - cocoa

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

Related

MPMoviePlayerController failed with itemFailedToPlayToEnd

I have retrieved movies file from my iPhone Photo Library, and saved them into my App`s Documents directory. When I click one movie file, I want to use MPMoviePlayerController to play it. Unfortunately, It fail to play the movie file with such error:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
I have searched for a long time to resolve this problem. Somebody say the format maybe is unsupport. The movies file is retrieved from Photo Library, so the format should be OK. Also, I copy the file to NSBundle and MPMoviePlayerController can play it. So the file is OK. Following is my codes:
NSString *newstr = [mFilePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
DLog(#"the raw is %# the newstr is %#", mFilePath, newstr);
NSURL *url = [NSURL URLWithString:newstr];
DLog(#"the url is %#", url);
#if 0
NSBundle *bundle = [NSBundle mainBundle];
if (bundle) {
NSString *path = [bundle pathForResource:#"test" ofType:#"MOV"];
if (path) {
url = [NSURL fileURLWithPath:path];
DLog(#"the new url is %#", url);
}
}
#endif
mPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[mPlayer setMovieSourceType:MPMovieSourceTypeFile];
[mPlayer prepareToPlay];
[mPlayer.view setFrame:self.view.bounds];
[mPlayer play];
[self.view addSubview:mPlayer.view];
Changing:
NSURL *url = [NSURL URLWithString:path];
to
NSURL *url = [NSURL fileURLWithPath:path];
fixed it for me, then:
mPlayer.contentURL = url;
[mPlayer prepareToPlay];
but you already do this correctly. I create the mPlayer once and then just shift the contentURL about (instead of alloc + initWithContentURL) for different videos. Have you tried that?

bundelPath without app name

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

Load .png into xcode and convert to UIImage

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

UIImageView's image disappears when changing the image

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

Open/edit a .txt file located in application bundle with NSTextView object

I would like to add an NSTextView object to my app and add an action that opens a .txt file located in the app bundle in the textView. Also - I would like to have the option to edit and save the edited doc without renaming it. So standard save, not save as.
What's the best way to handle this?
Use NSString to load the file and put it in your text view:
NSTextView *textView; //your NSTextView object
NSError *err = nil;
NSString *path = [[NSBundle mainBundle] pathForResource:#"EditableFile" ofType:#"txt"];
NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
if(!contents) {
//handle error
}
[textView setString:contents];
Saving is just the opposite. Get the string and write it to the file:
NSTextView *textView; //your NSTextView object
NSError *err = nil;
NSString *path = [[NSBundle mainBundle] pathForResource:#"EditableFile" ofType:#"txt"];
NSString *contents = [textView string];
if(![contents writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&err]) {
//handle error
}

Resources