I have a program that will be grabbing a image from our server for the app, however we want to save the image onto the iOS app for caching purposes.
The url would be similar to this.
http://www.example.com/image/app_name/mypicture.png
I need a way to get the image name (mypicture) and the extension (.png) into 2 different strings to save it.
How would I accomplish this using a NSscanner?
Thanks
You wouldn't use a scanner. Use the NSURl method absoluteString to get the path and then use NSString methods pathExtension, stringByDeletingPathExtension and finally lastPathComponent to get the extension and name. You could also use the NSURL method resourceValuesForKeys:error: to get the name with the NSURLLocalizedNameKey.
Related
I have an app that basically search youTube videos.
This all youTube search and the result videos are in one view controller.
Now, everything works fine and i do get the wanted videos (using JSON) displayed on my view using this API: "https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=%#"
The problems are:
1. UISearchBar accepts only one word to search with. if i put on the search bar two or more words, the app crashes. (if i don't want that the two words will make my app crash,
then i need to write on the searchBar, i.e "new%20songs" instead of "new songs").
so the question for here is: do i need to detect every space on the seachBar.text and replace it with "%20"?
or there is something more appropriate for that..?
another thing: language- my UISearchBar accepts only english! if i put another language on it-CRASH!
Do anyone have an explanation? i tried to change some of the API rules, but unsuccessfully.
EDIT: i'm not leaving here code, because i don't think it's the problem. the code works fine.. i think it's more like API problem, but of course, i'll post some code if needed.
Thanks to all !!!
To obtain the correct URL string you need to use stringByAddingPercentEscapesUsingEncoding method. So all spaces and characters (other than English) will be replaced by special codes (eg space will be replaced %20, etc.). Example:
NSString *searchStr = #"string with space";
NSString *youtubeURLString = [NSString stringWithFormat:#"https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=%#", searchStr];
NSString *URLString = [youtubeURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I need some help for my OS X program.
I need the URL of a file inside the supporting files.
I have an array in which I save URLs from images and add them to a table view and if no images are chosen I want to add a question mark image (it is called "bild.jpg")
This bild.jpg is inside the supporting files but for later use I can't just save the name of the image because the array stores also URLs.
I need to have the URL of that image in the supporting file because it's easier to use the array for image initialization.
Is there a function to get the path or is there a standard path to the supporting files? I already search on the net but couldn't find anything that could help.
You seem to be talking about the application bundle and its resources directory rather than, say, a subdirectory in ~/Library/Application Support/..., in which case you probably want something like:
[[NSBundle mainBundle] URLForResource:#"bild" withExtension:#"jpg"]
(See the documentation for NSBundle.)
I use below code for open image with NSOpenPanel but doesn't work
//panel=NSOpenPanel
NSString *imgg = [NSString stringWithFormat:#"%#",panel.URL];
self.imgUser.image=[NSImage imageNamed:imgg];
The problem is that +[NSImage imageNamed:] doesn't load an image by URL. If you read the documentation, it explains what it actually does: it looks for an image stored in the cache under that name, or stored in the app's bundle or AppKit's framework under that filename.
There are a large number of ways to actually open an image by URL. The one you're probably looking for is:
NSImage *image = [[[NSImage alloc] initWithContentsOfURL:panel.URL] autorelease];
Also, as a side issue, the way you're trying to turn a URL into a path is incorrect. If you have an NSURL for file://localhost/Users/user437064/Pictures/mypic.jpg, just converting that to a string just gives you #"file://localhost/Users/user437064/Pictures/mypic.jpg". That isn't a path that you can use with path-based APIs. What you actually want is #"/Users/user437064/Pictures/mypic.jpg", and the way you get that is -[NSURL path]. So "NSString *imgg = [panel.URL path];". But this is irrelevant; unless you need to deal with very old versions of OS X, or out-of-the-way APIs, there's almost always a method that takes a URL for each method that takes a path, and often the path-based ones are deprecated.
As an even farther-off-the-side issue, you don't need stringWithFormat: to convert something to a string; "[panel.URL description]" gives the exact same result as "[NSString stringWithFormat:#"%#", panel.URL]", much more simply and a little more efficiently.
I want to have a WebView that displays some static files from the application bundle. Since I have a large number of small files, I'd like to pack them all into a compressed archive so the application doesn't take up too much space. What's the best way to make this happen?
This should help you out: http://code.google.com/p/ziparchive/
To display data in the WebView:
On Mac OS X use WebFrame's loadHTMLString:baseURL:
On iOS use UIWebView's loadHTMLString:baseURL:
What you probably want to do, is implement an NSURLProtocol subclass that will resolve relative URLs by reading them from the zip archive. That way, you only need to initially read the "main" HTML file from the zip into memory, and the others will be read in on demand. To get WebKit to use your custom URL protocols for resolving relative paths, you could instantiate the WebView like this:
[[web_view mainFrame] loadHTMLString:your_main_html baseURL:[[NSURL alloc] initWithString:#"zip:///"]];
Apple has a really good example of combining a custom URL protocol with a WebView here:
https://developer.apple.com/library/mac/#samplecode/SpecialPictureProtocol/Introduction/Intro.html#//apple_ref/doc/uid/DTS10003816
I want to add a simple one-page HTML page help to my Cocoa app. Can you tell me how to do it? I assume I just have to throw in one lousy .html (and maybe one .css?) file somewhere into my Cocoa project in Xcode?
Creating Apple Help documents that are opened in the Help viewer is straightforward but you must follow the directions in the documentation exactly.
Help files are HTML but you need to place a couple of special tags in the page and name the files in a particular way.
It's all explained in the documentation.
Today I've been facing the same problem. I found no up-to-date howto so here is one of my own. Help is nicely working with this Step by Step to create Apple Help in your Cocoa Xcode Application.
If you only want a single HTML page and not a proper help file, you could add an HTML document and CSS file to your project. These will be copied to your application's Resources directory inside the app bundle when you compile the project. To load the document, you'll need to get its address. This is actually quite easy:
NSString *helpFilePath = [[NSBundle mainBundle] pathForResource:#"YourHelpDocumentHere" ofType:#"html"];
NSURL *helpFileURL = [NSURL fileURLWithPath:helpFilePath];
The resulting URL will be a file URL that a WebView can display inside your application, or you can pass it off to the operating system using NSWorkspace and it will be opened in the user's default web browser.