can't open image with NSOpenPanel - objective-c

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.

Related

Saving UIImage to File instead of NSUserDefaults

So I have an array with images that I want to store locally since downloading them each time will take unnecessary effort. I've read that I should NOT store images in NSUserDefaults, which is fine, but I can't for the life of me find any examples on how to store it as a file in a directory that does not change (iOS 8 changed the UUID with each build which creates a new folder each time I run it in Xcode).
I generally have two questions here:
Could someone help me translate this into ObjC? I can't comment on the post since I don't have enough rep...It's the swift part farther down the post that I need help with. Save images in NSUserDefaults?
The other question I have is that it seems to take a lot of time to save the data locally, no matter if it's to file or into the NSUserDefaults. What happens here is that the user of my app closes the app before the data has been stored locally. Is there any way to prevent this? I can add an ActivityIndicator, sure, but I can't seem to find any callback which tells me when the process of saving data has been completed.
Thanks!
So I managed to solve it. The problem was that I simply mixed up the paths. If anyone else wants the translation from Swift to ObjC from the link in the original post here it is:
Write
NSString* relativePath = [NSString stringWithFormat:#"image_%d.jpg", 1];
NSString* realPath = [self documentsPathForFileName:relativePath];
// Write image data to user's folder
[self.ImageData writeToFile:realPath atomically:YES];
// Store path in NSUserDefaults
[defaults setObject:relativePath forKey:#"path"];
Read
NSString *relativePath = [defaults objectForKey:#"path"];
NSString *realPath = [self documentsPathForFileName:relativePath];
self.ImageData = [NSData dataWithContentsOfFile:realPath];

Save in plist to a program directory

i need to save a .plist file NOT to documents, but to the core of program.
For example my program called "123" and if i save data, then send my app to my friend and he opens this app he could see saved data, no matter where he puts this program. I can't find solution to this problem, please help me.
I'm making mac app.
and i save plist with
[NSKeyedArchiver archiveRootObject:FBCover1.text=
[NSString stringWithFormat:#"%#",Cover1.attributedStringValue]
toFile:#"/Users/admin/FBCover1.plist"];
General answer:
If you're trying to do this on iPhone (you didn't tag this for iOS or MacOS), this isn't going to work as this will break your code signing.
If you're doing this on MacOS and you're using code signing, you'll have the same problem.
There may be places where you could save and share data, such as Game Center or DropBox or Box or some other cloud storage mechanism, but you'll need to pick up and make use of some additional API's or frameworks.
Specific answer just for you:
Instead of:
[NSKeyedArchiver archiveRootObject:FBCover1.text=[NSString stringWithFormat:#"%#",Cover1.attributedStringValue] toFile:#"/Users/admin/FBCover1.plist"];
which is big and ugly and I don't know what the heck it's doing, why not save your string this way?
NSString * stringToSave = [NSString stringWithFormat:#"%#",Cover1.attributedStringValue];
if(stringToSave)
{
NSError * error = nil;
BOOL success = [stringToSave writeToFile: #"/Users/admin/FBCovert1.txt" atomically: YES encoding: NSUTF8StringEncoding error: #error];
if(!success)
{
NSLog( #"error in saving - %#", [error localizedDescription]);
}
}
This saves the raw string into a file.
If you want to do it as a plist, then create a NSDictionary and save your string as the value with some appropriate key.
Preamble: this is an awful idea. What you should do is create a document-based application and pass your document backwards and forwards.
Literal answer:
You can use NSBundle to get the path of the resources folder within your application bundle with something like:
[[NSBundle mainBundle] resourcePath]
The resources folder is where application resources, such as plists, are meant to go. You're supposed to consider your application bundle as read-only in general but that's as good a choice as any if you want to hack away.

Array Doesn't load PNGs Exported from After Effects: ios, xcode

Our graphic designer is sending us .PNGs named appropiately "hide_00~iphone.png", "hide_00#2X~iphone.png" etc
He is exporting the images from after effects. I add them to the project and try to load them into an array on init. An exception is thrown each time for all of his files. Now, if I go in AND RENAME the files in the finder to exactly the same name, everything compiles fine. I have no idea what's going on here. Xcode cannot find them in the file system until I rename them. But the name is EXACTLY the same as what he sent me. I checked for white space around his file naming but everything looks fine.
Does anybody know if After Effects puts weird header info in the images? Or does this sound familiar at all to anyone. There are a whole bunch of images we are working with and I would hate to have to rename them by hand.
So I just used imageNamed and the image loaded just fine. So obviously your routine to load the image by path/name is flawed. You can post that and it can be evaluated - but this has nothing to do with 'After Effects'.
EDIT: For the record, my system is Lion Xcode 4.4.1 and my project set for iOS 5.1. I took your file from dropbox, and verified that in my project I CAN load the image as you are trying to do:
for (int i = 6; i < 7; i++) {
NSString *path = [NSString stringWithFormat:#"hide_step_seq_%02d", i];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:path ofType:#"png"]];
NSLog(#"PATH %# image=%#", path, image);
}
2012-09-13 07:15:23.241 Searcher[58114:f803] PATH hide_step_seq_06 image=<UIImage: 0x6a4cb30>
So, where to go from here? I've tried to help several people here who get burned by the #2x or ~iphone suffixes. For some reason a few people cannot ever seem to get this to work - all I can think of is there is some flag deep in the system that gets toggled and there is no way to untoggle it.
My suggestion is to try using the actual complete file name - try appending ~iphone and see if that works.
You can also in Terminal do a 'ls *.png | od -c' before changing the name and afterwards, to verify that absolutely the characters are the same.
The last thought I have on this is that files have many attributes: creation time, last access, last modiied, extended attributes, permissions, etc. It is possible (while unlikely) that for some reason one of these values blocks the system from attempting to use the ~iphone suffix.
I really wish I could help you further. If you want to put a simple little project together that does nothing more than tries to open a few images and it fails, zip the whole project up, put on dropbox, I'd be more than willing to run it on my system to try and duplicate the problem. You can also do as I did in the code above and verify that path looks good and the image is nil.

Parse Image and Extension From URL Objective C NSScanner

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.

URLCache - iPhone SDK

I need some help in using the NSURLCache to download a simple file. It would need to be saved in the Documents folder of the app. I have seen the URLCache example that apple presented, but I didn't find it useful since it was an image that was being shown. In addition there was some complex code on the modification date. I actually need something when the application opens it will begin the download process. Can someone guide me through just the download process of any file?
Thanks
Kevin
NSURL *myURL = [NSURL urlWithString:#"http://www.google.com/theInternet.zip"];
NSData *myData = [NSData dataWithContentsOfURL:myURL];
[myData writeToFile:#"ThePath" atomically:YES];
This will block, but should be ok for small downloads. If you are going to be downloading something that is many megabytes then look into asynchronous downloads. You can see an example here:
http://github.com/erica/iphone-3.0-cookbook-/tree/master/C13-Networking/07-Asynchronous%20Downloads/