Post

Replies

Boosts

Views

Activity

SwiftUI, macOS, PDFView, The "Remove Highlight" context menu does not work
I'm using PDFKitView: NSViewRepresentable to present the pdf page in SwiftUI. Seems we already have some useful built-in functions in the context menu. However, the highlight manipulation functions are not functional - I can neither delete the highlight annotation nor change the color/type of the current pointed highlight annotation. The "Add Note" and other page display changing functions work well.
1
1
895
12h
SwiftUI Menu always been refreshed and will interrupt user interaction
The demo code below presenting a view with a number incrementing per second. The problem is: Each time data updated in VM, the menu ( top line of MacOS) will also been refreshed, which will interrupt user's operation. Here is the code: *App.swift struct MemoryTestApp: App {   @ObservedObject var appVM = VM()   var body: some Scene {     WindowGroup {       ContentView(vm: appVM)         .environmentObject(appVM)     }     .commands {       CommandMenu("Menu1") {         Button("Test Btn") {           print("Test Btn pressed!")         }       }     }   } } VM class VM: ObservableObject {   @Published var count = 0       func startCount() async{     while 1 == 1 {       DispatchQueue.main.async {         self.count += 1         if self.count > 1000 {           self.count = 0         }       }       do{         try await Task.sleep(nanoseconds: 1_000_000_000)       }catch{}     }   } } View struct ContentView: View {   @ObservedObject var vm: VM   var body: some View {     Text("Hello, world! \(vm.count)")       .frame(width: 100, height: 100)       .onAppear(perform: {         Task{await vm.startCount()}       })   } } MacOS Monterey Xcode: 13.2.1
0
0
432
Dec ’21
CIFilter CIAreaMinMax gives incorrect result
Hi, I'm using filter CIAreaMinMax to get the brightest and darkest color information from an image. Normally the filter should output an image with two pixels (brightest and darkest). However, when I implement this filter to an image which contains two similar colors, then the result will be incorrect. The symptom of the incorrect result is, the two pixels' red channel has been switched, but G and B value have no problem. The test image I am using is, an png image only contains two color: RGB(37,62,88), GRB(10,132,255). After processed by the code, it will output an image which contains two pixels: RGB(10,62,88), GRB(37,132,255). In below is the test code for swift playground: import Cocoa import CoreImage import CoreGraphics func saveImage(_ image: NSImage, atUrl url: URL) {     let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil)!     let newRep = NSBitmapImageRep(cgImage: cgImage)     newRep.size = image.size     let pngData = newRep.representation(using: .png, properties: [:])!     try! pngData.write(to: url) } var sourceImage = CIImage.init(contentsOf: URL.init(fileURLWithPath: "/Users/ABC/Downloads/test.png"))! let filter = CIFilter(name: "CIAreaMinMax")! filter.setValue(sourceImage, forKey: kCIInputImageKey) let civ = CIVector.init(x: sourceImage.extent.minX, y: sourceImage.extent.minY, z: sourceImage.extent.width, w: sourceImage.extent.height) filter.setValue(civ, forKey: kCIInputExtentKey) var filteredImage = filter.outputImage! let context = CIContext(options: [.workingColorSpace: kCFNull!]) let filteredCGImageRef = context.createCGImage(     filteredImage,     from: filteredImage.extent) let output = NSImage(cgImage: filteredCGImageRef!, size: NSSize.init(width: filteredImage.extent.width, height: filteredImage.extent.height)) saveImage(output, atUrl: URL.init(fileURLWithPath: "/Users/ABC/Downloads/output.png"))
2
0
1.1k
Nov ’21
SwiftUI, macOS, PDFView, The "Remove Highlight" context menu does not work
I'm using PDFKitView: NSViewRepresentable to present the pdf page in SwiftUI. Seems we already have some useful built-in functions in the context menu. However, the highlight manipulation functions are not functional - I can neither delete the highlight annotation nor change the color/type of the current pointed highlight annotation. The "Add Note" and other page display changing functions work well.
Replies
1
Boosts
1
Views
895
Activity
12h
SwiftUI Menu always been refreshed and will interrupt user interaction
The demo code below presenting a view with a number incrementing per second. The problem is: Each time data updated in VM, the menu ( top line of MacOS) will also been refreshed, which will interrupt user's operation. Here is the code: *App.swift struct MemoryTestApp: App {   @ObservedObject var appVM = VM()   var body: some Scene {     WindowGroup {       ContentView(vm: appVM)         .environmentObject(appVM)     }     .commands {       CommandMenu("Menu1") {         Button("Test Btn") {           print("Test Btn pressed!")         }       }     }   } } VM class VM: ObservableObject {   @Published var count = 0       func startCount() async{     while 1 == 1 {       DispatchQueue.main.async {         self.count += 1         if self.count > 1000 {           self.count = 0         }       }       do{         try await Task.sleep(nanoseconds: 1_000_000_000)       }catch{}     }   } } View struct ContentView: View {   @ObservedObject var vm: VM   var body: some View {     Text("Hello, world! \(vm.count)")       .frame(width: 100, height: 100)       .onAppear(perform: {         Task{await vm.startCount()}       })   } } MacOS Monterey Xcode: 13.2.1
Replies
0
Boosts
0
Views
432
Activity
Dec ’21
CIFilter CIAreaMinMax gives incorrect result
Hi, I'm using filter CIAreaMinMax to get the brightest and darkest color information from an image. Normally the filter should output an image with two pixels (brightest and darkest). However, when I implement this filter to an image which contains two similar colors, then the result will be incorrect. The symptom of the incorrect result is, the two pixels' red channel has been switched, but G and B value have no problem. The test image I am using is, an png image only contains two color: RGB(37,62,88), GRB(10,132,255). After processed by the code, it will output an image which contains two pixels: RGB(10,62,88), GRB(37,132,255). In below is the test code for swift playground: import Cocoa import CoreImage import CoreGraphics func saveImage(_ image: NSImage, atUrl url: URL) {     let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil)!     let newRep = NSBitmapImageRep(cgImage: cgImage)     newRep.size = image.size     let pngData = newRep.representation(using: .png, properties: [:])!     try! pngData.write(to: url) } var sourceImage = CIImage.init(contentsOf: URL.init(fileURLWithPath: "/Users/ABC/Downloads/test.png"))! let filter = CIFilter(name: "CIAreaMinMax")! filter.setValue(sourceImage, forKey: kCIInputImageKey) let civ = CIVector.init(x: sourceImage.extent.minX, y: sourceImage.extent.minY, z: sourceImage.extent.width, w: sourceImage.extent.height) filter.setValue(civ, forKey: kCIInputExtentKey) var filteredImage = filter.outputImage! let context = CIContext(options: [.workingColorSpace: kCFNull!]) let filteredCGImageRef = context.createCGImage(     filteredImage,     from: filteredImage.extent) let output = NSImage(cgImage: filteredCGImageRef!, size: NSSize.init(width: filteredImage.extent.width, height: filteredImage.extent.height)) saveImage(output, atUrl: URL.init(fileURLWithPath: "/Users/ABC/Downloads/output.png"))
Replies
2
Boosts
0
Views
1.1k
Activity
Nov ’21