Posts

Post marked as solved
16 Replies
2.7k Views
Hi all, can someone more experienced or smarter than me please take a look at this example code? I was struggling with performance issues using SwiftUI with an MTKView and found out that the Struct I was using to keep the bindings was the guilty one. After hours of searching and trying stuff, I found out that using a Class solved the performance issues. It introduces other issues though, like the UI not always updating everywhere, and all my sliders returning to zero when my macOS app window is minimised or has no focus. Even though I understand the difference between value and reference types, and that copying a Struct each time takes CPU cycles, I don't understand why my Class values seem to be unreachable at times by the UI... Here's the macOS example code: // //	ContentView.swift //	SlowStructFastClass // //	Created by Michel Storms on 12/01/2021. // import SwiftUI class VC { 		var s1: Double = 0.5 } struct ContentView: View { 		@State var valueStruct: Double = 0.5 		@State var valueClass = VC() 		 		var body: some View { 				VStack { 						Text("STRUCT") 						Slider(value: $valueStruct) 						Text("\(self.valueStruct)") 						 						Text("") 						 						Text("CLASS") 						Slider(value: $valueClass.s1) 						Text("\(self.valueClass.s1)") 				} 		} } I think this is the simplest I could make the example, yet there is still a big speed difference between struct and class binding-based sliders... Any clues? What am I doing wrong? Thanks for your time! (tested on a 2020 i5 MBP running Big Sur)
Posted Last updated
.
Post marked as solved
2 Replies
1k Views
Hi all, my macOS project needs a way to save images in multiple formats. The code to export a CIImage to these formats is in place and working, but I'm struggling with getting my NSSavePanel() to display the file type selector you see on most apps. I found out that IKSaveOptions() can be used to add this to an existing NSSavePanel(), but havent found any examples on how to implement this. Please take a look at my code, and let me know if something rings a bell? Thanks in advance! swift private func saveToFile() {         let panel = NSSavePanel()         let options = IKSaveOptions()         options.addAccessoryView(to: panel) // seems to get ignored I then show the panel with this code. The panel is working, but never shows the file selector controls: swift panel.begin { response in if response == NSApplication.ModalResponse.OK, let savePath = panel.url { let nsImage = NSImage(cgImage: cgImage!, size: ciImage.extent.size) if nsImage.pngWrite(to: savePath) { print("File saved") } } }
Posted Last updated
.
Post marked as solved
2 Replies
1.8k Views
Hi, I have this piece of code to bind the value of one slider in SwiftUI. It works great for one slider, I can set it and keep track of the value in a text field. I am working on an app that has multiple sliders, and I am kind of lost on how to keep track of multiple ones? Do I need to keep this in a class or struct? Any help or pointing in the right direction is highly appreciated! struct ContentView: View {     @State private var sliderLevel = 1.0     var body: some View {         let intensity = Binding<Double>(             get: {                 self.sliderLevel             },             set: {                 self.sliderLevel = $0 self.applyProcessing()             }         )
Posted Last updated
.