Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't load images on mac screensaver release build (it works on Xcode debug build)

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)

Debug Build Screenshot

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.)

like image 405
Ahmadreza Avatar asked Feb 23 '20 05:02

Ahmadreza


People also ask

What is the difference between debug and release in Xcode?

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.

How do I run a release build in Xcode?

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.

How do I debug a background process in Xcode?

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.

What are the xcode project’s Build settings?

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.


1 Answers

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.

like image 177
Asperi Avatar answered Sep 27 '22 19:09

Asperi