2011-09-19 174 views
1

如何让我的用户上传照片并设置图像以及打开文件对话框

- (IBAction)chooseFile:(id)sender { 
    int i; // Loop counter. 

    // Create the File Open Dialog class. 
    NSOpenPanel* openDlg = [NSOpenPanel openPanel]; 

    // Enable the selection of files in the dialog. 
    [openDlg setCanChooseFiles:YES]; 

    // Enable the selection of directories in the dialog. 
    [openDlg setCanChooseDirectories:YES]; 

    // Display the dialog. If the OK button was pressed, 
    // process the files. 
    if ([openDlg runModalForDirectory:nil file:nil] == NSOKButton) 
    { 
     // Get an array containing the full filenames of all 
     // files and directories selected. 
     NSArray* files = [openDlg filenames]; 

     // Loop through all the files and process them. 
     for(i = 0; i < [files count]; i++) 
     { 
      NSString* fileName = [files objectAtIndex:i]; 
      // Do something with the filename 
[customButtonImg setImage:[NSImage imageNamed:fileName]]; 

     } 
    } 
} 
+1

runModalForDirectory:file:types:在OS X v10.6中已弃用。你可以改用runModal。您可以使用setDirectoryURL设置路径:,您可以使用setAllowedFileTypes设置fileTypes :. – Itachi

回答

1

的图像追随this,它可能是引导你...

3
static NSArray * openFiles() 
{ 
    NSArray *fileTypes = [NSArray arrayWithObjects:@"jpg",@"jpeg",nil]; 
    NSOpenPanel * panel = [NSOpenPanel openPanel]; 
    [panel setAllowsMultipleSelection:NO]; 
    [panel setCanChooseDirectories:NO]; 
    [panel setCanChooseFiles:YES]; 
    [panel setFloatingPanel:YES]; 
    NSInteger result = [panel runModalForDirectory:NSHomeDirectory() file:nil 
             types:fileTypes]; 
    if(result == NSOKButton) 
    { 
     return [panel URLs]; 
    } 
return nil; 
} 

-(IBAction)buttonloadImage:(id)sender 
{ 
    NSArray * paths = openFiles(); 

    if(paths) 
    { 
NSImage * aimage = [[NSImage alloc] initWithContentsOfURL:[paths objectAtIndex:0]]; 
     [aImageView setImage:aimage]; 
    } 
    } 
4
NSOpenPanel* openDlg = [NSOpenPanel openPanel] 

[openDlg setPrompt:@"Select"]; 

NSArray* imageTypes = [NSImage imageTypes]; 

[openDlg setAllowedFileTypes:imageTypes]; 

[openDlg beginWithCompletionHandler:^(NSInteger result){ 
    NSArray* files = [openDlg filenames]; 
    NSData *imgData; 
    for(NSString* filePath in files) 
    { 
     NSURL *url = [[NSURL alloc]initFileURLWithPath:filePath]; 
     NSImage *img; 
     if(url) 
     { 
      img = [[NSImage alloc]initWithContentsOfURL:url]; 
      imgData = [NSData dataWithContentsOfURL:url]; 
      [url release]; 
     } 
     if(img) 
     { 
       youimageView.image = img; 

      [img release]; 
     } 
     else 
     { 
       youimageView.image = nil; 


      NSAlert *alert = [[NSAlert alloc]init]; 
      [alert setMessageText:@"Application Message"]; 
      [alert setAlertStyle:NSInformationalAlertStyle]; 
      [alert setInformativeText:@"Select Only Image"]; 
      [alert beginSheetModalForWindow:self.view.window 
           modalDelegate:self didEndSelector:nil contextInfo:nil]; 
     } 

     NSLog(@"%@",filePath); 
     //do something with the file at filePath 
    } 
}];