Open File Dialog

15,167

Solution 1

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

Solution 2

 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];
    }
  }
Share:
15,167
Admin
Author by

Admin

Updated on August 29, 2022

Comments

  • Admin
    Admin over 1 year

    How do I allow my user to upload a photo and set the image of an Image well

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