打开文件对话框

发布于 2024-12-05 01:19:06 字数 1010 浏览 0 评论 0原文

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

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

        }
    }
}

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

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停滞 2024-12-12 01:19:06
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
    }
}];
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
    }
}];
帝王念 2024-12-12 01:19:06
 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];
    }
  }
 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];
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文