-(IBAction)showOpenPanel:(id)sender{NSOpenPanel*panel=[NSOpenPanelopenPanel];[panelsetDirectory:NSHomeDirectory()];// Set panel's default directory.[panelsetAllowedFileTypes:[NSImageimageFileTypes]];// Set what kind of file to select.// More panel configure code.[panelbeginSheetModalForWindow:[myViewwindow]completionHandler:(^(NSIntegerresult){if(result==NSOKButton){NSArray*fileURLs=[panelURLs];NSImage*image=[[NSImagealloc]initWithContentsOfURL:[fileURLsobjectAtIndex:0]];NSLog(@"%@",[fileURLsobjectAtIndex:0]);// Deal with the image.[imagerelease];}})];}
用这个新方法利用了Objective-C 2.0的新特征:Block。在引入了Block之后,这个方法也就变成了多线程的了——这是Grand Central Dispatch的使用实例之一。