我希望能够在Swift中打开图像。这是我的第一个Swift项目。
@IBAction func SelectFileToOpen(sender: NSMenuItem) {
var openPanel = NSOpenPanel();
openPanel.allowsMultipleSelection = false;
openPanel.canChooseDirectories = false;
openPanel.canCreateDirectories = false;
openPanel.canChooseFiles = true;
let i = openPanel.runModal();
if(i == NSOKButton){
print(openPanel.URL);
var lettersPic = NSImage(contentsOfURL: openPanel.URL!);
imageView.image = lettersPic;
}
}
使用开放式面板时输出NSLog
Optional(file:///Users/ethansanford/Desktop/BigWriting.png)
fatal error: unexpectedly found nil while unwrapping an Optional value
如何让用户打开感兴趣的png文件。 当我在代码中指定相同的文件时一切正常。我的一个示例,指示在不使用打开文件面板并充当用户的情况下在代码中打开哪个文件:
let pictureURl = NSURL(fileURLWithPath: "///Users/ethansanford/Desktop/BigWriting.png");
var lettersPic = NSImage(contentsOfURL: pictureURl!);
imageView.image = lettersPic;
我的网址格式有问题吗?任何帮助,将不胜感激。
答案 0 :(得分:16)
将新文件添加到项目中(swift源文件)并在此处添加此扩展名
Xcode 9•Swift 4
extension NSOpenPanel {
var selectUrl: URL? {
title = "Select Image"
allowsMultipleSelection = false
canChooseDirectories = false
canChooseFiles = true
canCreateDirectories = false
allowedFileTypes = ["jpg","png","pdf","pct", "bmp", "tiff"] // to allow only images, just comment out this line to allow any file type to be selected
return runModal() == .OK ? urls.first : nil
}
var selectUrls: [URL]? {
title = "Select Images"
allowsMultipleSelection = true
canChooseDirectories = false
canChooseFiles = true
canCreateDirectories = false
allowedFileTypes = ["jpg","png","pdf","pct", "bmp", "tiff"] // to allow only images, just comment out this line to allow any file type to be selected
return runModal() == .OK ? urls : nil
}
}
在视图控制器中:
class ViewController: NSViewController {
@IBOutlet weak var imageView: NSImageView!
@IBAction func saveDocument(_ sender: NSMenuItem) {
print("SAVE")
}
@IBAction func newDocument(_ sender: NSMenuItem) {
print("NEW")
}
// connect your view controller to the first responder window adding the openDocument method
@IBAction func openDocument(_ sender: NSMenuItem) {
print("openDocument ViewController")
if let url = NSOpenPanel().selectUrl {
imageView.image = NSImage(contentsOf: url)
print("file selected:", url.path)
} else {
print("file selection was canceled")
}
}
}
答案 1 :(得分:4)
嗯......我没有看到你的代码有什么问题,所以我测试运行这段代码(在桌面上选择一个PNG文件):
let openPanel = NSOpenPanel()
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
openPanel.canCreateDirectories = false
openPanel.canChooseFiles = true
let i = openPanel.runModal()
if(i == NSModalResponseOK){
print(openPanel.URL)
let lettersPic = NSImage(contentsOfURL: openPanel.URL!)
print(lettersPic)
}
我得到的是:
可选(文件:///Users/jwlaughton/Desktop/flame%2012-32.png)
可选的( “NSBitmapImageRep 0x6000000a4140 Size = {1440,900} ColorSpace =(尚未加载)BPS = 8 BPP =(尚未加载)Pixels = 1440x900 Alpha = NO Planar = NO格式=(尚未加载)CurrentBacking = nil(断层) CGImageSource = 0x608000160cc0“)>)
对我来说似乎没问题。
也许问题是你需要说:
imageView.image = lettersPic!;
编辑:
因此,为了进一步测试,我将测试代码扩展到:
if(i == NSOKButton){
print(openPanel.URL);
var lettersPic = NSImage(contentsOfURL: openPanel.URL!);
print(lettersPic);
let view:NSImageView = NSImageView();
view.image = lettersPic
print(view)
}
一切仍然有效。抱歉,我无法复制您的问题。
答案 2 :(得分:1)
这是最终为我工作的代码。我不得不禁用故事板。我不得不打造一个名为Main的课程。这不要与一个名为main.swift的特殊类混淆,后者替换了appdelegate.swift。我还必须导入Cocoa。然后我不得不指定主要来自nsobject。这样我就可以先在界面构建器之间建立连接,并在我的Main.swift文件中放入Inbbactions和outlet。
//
// Main.swift
// Open
//
// Created by ethan sanford on 2015-01-18.
// Copyright (c) 2015 ethan D sanford. All rights reserved.
//
import Foundation
import Cocoa
class Main: NSObject{
@IBOutlet var imageWell: NSImageCell!
var myURL = NSURL(fileURLWithPath: "")
@IBAction func main(sender: AnyObject) {
imageWell.image = NSImage(byReferencingURL: myURL!)
}
@IBAction func open(sender: AnyObject) {
var openPanel = NSOpenPanel();
openPanel.allowsMultipleSelection = false;
openPanel.canChooseDirectories = false;
openPanel.canCreateDirectories = false;
openPanel.canChooseFiles = true;
let i = openPanel.runModal();
if(i == NSOKButton){
print(openPanel.URL);
myURL = openPanel.URL;
}
}
}
它有点奇怪,你必须选择你的文件点击打开。然后点击与@IBAction func main(sender: AnyObject) {