打开文件对话框

时间:2011-09-19 05:16:24

标签: objective-c macos openfiledialog

如何让我的用户上传照片并设置图像的图像

- (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]];

        }
    }
}

3 个答案:

答案 0 :(得分: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
    }
}];

答案 1 :(得分: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];
    }
  }

答案 2 :(得分:1)

关注this,它可能会指导您......