I got this mac screensaver example from here.
I changed it to show image instead of textField.
The problem is this: it can show any Xcode objects such as textView. textField, Button and etc... but it cant load image on imageView. (it can load image on Xcode debug build but not on release build on mac screensavers)
Release Build Screenshot Here is my code to show image:
let imageView = NSImageView(frame: self.frame)
imageView.image = NSImage(named: "Image")
imageView.layer?.backgroundColor = NSColor.red.cgColor
imageView.backgroundColor(.red)
addSubview(imageView)
DispatchQueue.main.async {
imageView.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
self.setNeedsDisplay(imageView.frame)
}
I am pretty sure it can not access the image file, but how can I fix this? Actually the behavior of screensaver is very weird, when I add another UI element after that ImageView, neither of them showing on the screen.
Whats wrong with macOS screensavers? (I downloaded some third party screensavers from some websites & they were black screens as well.)
When you create a new project in Xcode, it defines two build configurations, Debug and Release. By default, Debug configuration is used during development whereas the Release configuration is used for TestFlights or App Store.
Xcode allows development and release build settings to diverge, and disables watchdog timeouts. To test the exact conditions your app user’s experience, create a release build. In your Xcode project’s scheme editor, set the run destination to a device and adjust the archive task to the Release configuration.
Xcode’s debugger prevents apps from being suspended, so launch your app from the home screen to test background processes. For example, NSURLSession implements a background session, but when the app runs in the Xcode debugger, the background session never runs.
The Xcode project’s Build Settings support different values based on the active build configuration. The default build configurations are Debug and Release, which the project scheme maps to the Run task and Archive task, respectively.
NSImage(named:)
loads from main application bundle, but your images in plug-in, so use something like the following (load from bundle containing your class)
imageView.image = Bundle(for: type(of: self)).image(forResource: "Image")
Tested with Xcode 11.2 / macOS 10.15. Images are in Assets of screensaver plug-in target.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With