I've read several questions about this, but no answer seems to fit. If I want to open maps from my app, with an arbitrary location defined by coordinates and an arbitrary name, to use as a title in a pin dropped in that location, how can I do it? With iOS5 I used the openURL with an url scheme built this way:
maps:q=Arbitrary+name#45.000,45.000
This doesn't work anymore (thus all the apps to be updated!), and I don't seem to find a way to achieve the same behaviour: the name I provide is always used as a search parameter, and if nothing is found the pin won't be dropped. On the other side, any found result seems to override coordinates, so several pin are dropped in another part of the globe.
I am trying even formats such:
http://maps.apple.com/?sll=45.000,45.000&q=Arbitrary+name
Any help?
This can be done by the following way:
CLLocationCoordinate2D coOrdinates = //
MKPlacemark *location = [[MKPlacemark alloc] initWithCoordinate:coOrdinates addressDictionary:nil];
MKMapItem *pinItem = [[MKMapItem alloc] initWithPlacemark:endLocation];
pinItem.name =#"Location";
NSMutableDictionary *launchOptions = [[NSMutableDictionary alloc] init];
[pinItem openInMapsWithLaunchOptions:launchOptions];
[location release];
[launchOptions release];
[pinItem release];
Related
as always relying on this community wich is always really helpful!
I really don't understand why my code isn't working, i am playing the game in ViewControllerA for example, there is "movements" and more stuff shown,wich are labels with int variables shown, this ones give me no problem because they change correctly,when the game is completed i want to show the score and more...
So i created another ViewController to do it, ViewControllerB for the example, the thing is that A and B Viewcontrollers belong to the SAME class, so i should be able to use the same variables, methods and everything ( actually i do that already and it's working fine)
I created IBOutlet for some labels, this ones belong to B. I am using presentViewcontroller method to show it up,the labels don't get anything changed, here is the code:
NSString *moviments = [NSString stringWithFormat:#"%d",TotalMovements];
NSString *score = [NSString stringWithFormat:#"%d",TotalPoints];
NSString *levelToShow = [NSString stringWithFormat:#"%d",PlayingLevel];
[movementsToShowOnCompletedScreen setText:moviments];
[scoreToShowOnCompletedScreen setText:score];
[levelToShowOnCompletedScreen setText:levelToShow];
UIStoryboard *winOFLevelStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [winOFLevelStoryboard instantiateViewControllerWithIdentifier:#"mainGame"];
[self presentViewController:vc animated:YES completion:^{
/*I have tried to add the same code from above setting the labels text
labels here, same result...
*/
}];
The labels don't get its text changed.
This is it, i hope with all of this you can find out what's going on here, the IBOutlets are fine, i even changed programmatically the frame of those labels before so that can't be the problem, outlets are fine.
If you need more code or anyhthing just let me know!
Thanks for your time !
Well, i found it...just in case anyone has the same problem. It solves it but not the way i wanted to so if anyone knows the good way please let me know.
Since those belong to the same class i have just copied/pasted this lines shown below to my viewdidload method, now labels are changing when they have to but i really think this is a really poor solution.
Thanks again!
I try to get a file icon and to set it back to the same file (goal is to have overlay, but I first want to have this one work):
NSImage *img=[[NSWorkspace sharedWorkspace] iconForFile:#"MyFilePath"];
NSLog(#"x=%.f",img.size.width); // Result=32
[[NSWorkspace sharedWorkspace] setIcon:img forFile:#"MyFilePath" options:0];
-> Result is that my file gets a standard Finder icon instead of keeping its own icon. Anything I am doing wrong ?
Try using it like this:
[[NSWorkspace sharedWorkspace]
setIcon: [[NSImage alloc] initWithContentsOfFile:#"MyFilePath"]
forFile: #"MyFilePath"
options: 0];
You need to to load the icon/image into memory first.
EDIT: Answer updated to provide additional information in regards to
the comment below.
"how can I keep the image in memory to "play" with it before reassigning it?"
The NSImage that gets allocated into memory from your specified path can be manipulated in just about any possible way once it's been loaded. You'll want to thoroughly read the NSImage Class Reference to gain a real understanding of what it does and how use it's methods. For this particular scenario you'll want to be able have a named variable assigned from the icon you load.
Only one change needs to happen with the code above to make it work:
NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile:#"MyFilePath"];
[[NSWorkspace sharedWorkspace]
setIcon: iconImage
forFile: #"MyFilePath"
options: 0];
NSLog(#"iconImage: %#",iconImage);
The slight change essentially assigns the variable iconImage from the NSImage icon; everything else stays the same. The NSLog will give you a very quick glimpse at properties associated with iconImage — where you take it from there is really up to your coding ability and creativity.
My application is trying to create custom objects from NSImage objects (coming from the pasteboard) but my current process only takes in image URLs.
I'd like to avoid major changes at this point so I was wondering if there was any way to get the URL of an NSImage (it seems like a reasonable expectation since one can initialize an NSImage from a URL)
Thanks.
EDIT (answer)
I went a slightly different route. Instead of getting the content of the pasteboard as an array of NSImage, I simply got it as an array of NSURL. I can then feed those into my process.
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *classArray = [NSArray arrayWithObject:[NSURL class]];
NSDictionary *options = [NSDictionary dictionary];
BOOL ok = [pasteboard canReadObjectForClasses:classArray options:options];
if (ok) {
NSArray *URLs = [pasteboard readObjectsForClasses:classArray options:options];
}
Quote by BlazingFrog:
(it seems like a reasonable expectation since one can initialize an NSImage from a URL)
Lets say I initialize a NSString by using:
NSString * theString = [NSString initWithContentsOfURL: encoding: error: ];
I'm sure it's not possible to retrieve the original NSURL from the NSString.
And I'm quite sure the same applies to NSImage. (Actually, completely sure.)
Indeed NSImage can be initialized by initWithContentsOfURL:.
But it can also be initialized by initWithData: or initWithPasteboard:.
The NSURL is no strict requirement for initializing a NSImage.
In other words, the NSImage might be initialized without using a URL.
The NSImage is simply a container for image representations.
Quote by Apple:
An NSImage object manages a group of image representations.
Solutions
Change you 'process' to accept NSImage.
Write the NSImage to a temporary file and use that file path.
If the image is being delivered via the standard pasteboard (i.e. the copy/paste mechanism) then there is no way to refer to it by URL because it might not have one. For instance, if you open a document in Word or Pages, select an image and copy it there is no possible way to create a URL reference to that image. It's on the pasteboard but not in the file system in a form you can access.
I think that you're going to have to modify your code to handle NSImage objects directly.
I need to get the URLs of all files dragged/dropped into my application from Finder.
I have a Cocoa app running on 10.6 which does this by using the new 10.6 NSPasteboard APIs which handle multiple items on the pasteboard. I'm trying to backport this app to 10.5. How do I handle this on 10.5?
If I do something like below, I only get the first URL:
NSArray *pasteTypes = [NSArray arrayWithObjects: NSURLPboardType, nil];
NSString *bestType = [pboard availableTypeFromArray:pasteTypes];
if (bestType != nil) {
NSURL *url = [NSURL URLFromPasteboard:pboard];
}
Getting multiple filenames is easy: (While getting multiple URLs is not with 10.5)
Register your view for
NSFilenamesPboardType
In performDragOperation: do the following to get an array of file paths:
NSPasteboard* pboard = [sender draggingPasteboard];
NSArray* filenames = [pboard propertyListForType:NSFilenamesPboardType];
The IKImageKit programming topics outline a way to do this like so (paraphrased):
NSData *data = [pasteboard dataForType:NSFilenamesPboardType];
NSArray *filenames = [NSPropertyListSerialization
propertyListFromData:data
mutabilityOption:kCFPropertyListImmutable
format:nil
errorDescription:&errorDescription];
See here: Image Kit Programming Guide: Supporting Drag and Drop
The NSURLPboardType just handles one URL.
To get a list of files you need to create a NSArray from a NSFilenamesPboardType.
Apple's docs on drag and drop are pretty good, even if it's older stuff.
How do I handle [multiple items on a pasteboard] on 10.5?
Try the Pasteboard Manager.
The tricky part is that you're handling a drop, which means you're receiving an NSPasteboard already created for you, and there's no way to convert between NSPasteboard objects and PasteboardRefs. You'll have to ask the NSPasteboard for its name, then pass the same name to PasteboardCreate, and that may not work.
my two cents for swift 5.1 (drop in NSView... to be customized)
see at:
Swift: Opening a file by drag-and-drop in window
This is probably a n00b question so I apologize in advance. I'm working with NSImage for the first time and basically I need to simply take a picture that is in my Resources folder, and have it display in an NSView/NSImageWell when a button is clicked.
NSImage *image = [[NSImage alloc] initWithContentsOfFile:#"tiles.PNG"];
if ( [image isValid] ) {
NSImageView *view = [[NSImageView alloc] init];
[selection setImage:image];
[selection setImageScaling:NSScaleProportionally];
}
else {
--- This line of code always activates meaning my image isn't valid
}
My only guess is that I am getting the path wrong to the image file and I have looked all over for the right way to access it. Another guess is that I have my code wrong. Anybody familiar with this? Thanks!
I work a lot more with the iPhone, but initWithContentsOfFile seems to require a full/relative path, which I assume tiles.PNG wouldn't fulfill.
I'd use the class method imageNamed:(NSString *)name, which will search your bundle for you.
You should use NSBundleManger to locate the image like so:
NSBundle *mb=[NSBundle mainBundle];
NSString *fp=[mb pathForResource:#"titles" ofType:#"PNG"];
UIImage *img=[UIImage imageWithContentsOfFile:fp];
That way you don't have to mess with internal paths yourself. Otherwise, you have to have the path relative to the final built product which is hard to create and maintain.