简体   繁体   English

IKImageView不显示图片(可可雨燕)

[英]IKImageView don't show the picture (Cocoa Swift)

I'm trying to show an image from file url in IKImageView, but it only shows an empty frame. 我正在尝试从IKImageView中的文件URL中显示图像,但它仅显示一个空框架。 My project is in Cocoa Swift 4.2 MacOS on Xcode 10.0. 我的项目在Xcode 10.0上的Cocoa Swift 4.2 MacOS中。

My code: 我的代码:

class ViewController: NSViewController {

    @IBOutlet weak var imagePreView: IKImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        imagePreView.wantsLayer = true

        imagePreView.setImageWith(URL(fileURLWithPath: "My_file_URL"))
        imagePreView.isHidden = false

    }

    override var representedObject: Any? {
        didSet {
            // Update the view, if already loaded.
        }
    }

}

Am I doing something wrong or is there missing something? 我是在做错事还是缺少什​​么? I have searching for hours, but I still haven't found anything usefull about IKImageView in Swift or they are all out of date, like this . 我已经搜索了几个小时,但是我仍然没有在Swift中找到关于IKImageView的有用信息,或者它们都已经过时了,就像这样

Expanding on @Willeke's comment above, the reason this didn't work was that the app (by default) is sandboxed and doesn't have access to random files on disk. 扩展上面@Willeke的评论,此操作不起作用的原因是该应用程序(默认情况下)已被沙箱化,并且无法访问磁盘上的随机文件。

If you run into issues like this, NSImage , IKImageView , etc will all silently fail when trying to load an image from disk. 如果遇到这样的问题,则在尝试从磁盘加载映像时, NSImageIKImageView等都会全部失败。

If you try using FileManager or Data(contentsOf: url) then you'll start seeing the real error, which is a permissions exception. 如果尝试使用FileManagerData(contentsOf: url)则将开始看到真正的错误,这是权限异常。

Turning off sandboxing does fix this, but if you're interested in learning how to grant access to read files on disk, I wrote a detailed blog post on the topic: 关闭沙箱确实可以解决此问题,但是如果您有兴趣学习如何授予访问磁盘上文件的权限,我就此主题撰写了详细的博客文章:

Troubleshooting AppKit File Permissions . 对AppKit文件权限进行故障排除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM