As a newbie to the big wide world of Swift and Xcode I am grappling with hopefully a simple, easy to solve problem with a macOS app in the very initial stage of development. However, thus far I have spent ages on it and am getting nowhere!
I have a ViewController (descended from NSViewController) which has an NSImageView which, on application start-up, should show (via an NSImage) a picture of a horse read from disk (from file /tmp/horse2.jpg). However, the image doesn't show.
As per the code listing below, the ViewController method setImageOfImageViewer() is responsible for reading the contents of the file /tmp/horse2.jpg and setting the image of the NSImageView instance (imageView).
The method setImageOfImageViewer() is called from viewDidLoad() and also later, whenever the user presses a button.
Among others, jpg files can be dragged from Finder on to the NSImageView. These images do show (every time). However, when the first of these files is so dragged and dropped, the following problem is reported (just once - and only in response to the first drag and drop):
2018-02-14 08:05:05.453636+1030 Image_Digitiser[4835:117719] MessageTracer: load_domain_whitelist_search_tree:73: Search tree file's format version number (0) is not supported
2018-02-14 08:05:05.453681+1030 Image_Digitiser[4835:117719] MessageTracer: Falling back to default whitelist
Pressing the button repeatedly fails to show the horse image, until AFTER the horse image file itself is dragged and dropped on to the NSImageView. After dragging other images on to the NSImageView, clicking the button does now reshow the horse image (repeatedly).
Grateful if you can you help me overcome this problematic behaviour.
For your information, the class ViewController is shown below. The ViewController simply contains three outlets: an NSTextField, NSButton and NSImageView plus one action to respond to the button being clicked.
import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var lblHello: NSTextField!
@IBOutlet weak var btnChangeLabel: NSButton!
@IBOutlet weak var imageView: NSImageView!
@IBAction func btnChangeLabelOnClick(_ sender: Any) {
lblHello.stringValue = "Horse image should now be showing!"
setImageOfImageViewer()
}
// Problem: This routine succeeds in showing the image "/tmp/horse2.jpg"
// ONLY AFTER the file is dragged on to the ImageView from Finder!!
fileprivate func setImageOfImageViewer() {
let imagePath = "/tmp/horse2.jpg"
//let imageUrl = NSURL(fileURLWithPath: imagePath)
//let myImage = NSImage(named: NSImage.Name("test")) // Works - does show that image on app start-up.
//let myImage = NSImage(byReferencingFile: imagePath) // Doesn't show the image on app start-up.
//let myImage = NSImage(byReferencing: imageUrl as URL) // Doesn't show the image on app start-up.
let myImage = NSImage(contentsOfFile: imagePath) // Doesn't show the image on app start-up.
print("myImage?.isValid = \(myImage?.isValid)") // Prints nil when image doesn't show, Optional(true) otherwise
imageView.image = myImage
imageView.isEditable = true
}
override func viewDidLoad() {
super.viewDidLoad()
lblHello.stringValue = "Hello World"
btnChangeLabel.title = "Show horse image"
setImageOfImageViewer()
}
override func viewWillAppear() {
super.viewWillAppear()
preferredContentSize = view.fittingSize
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}