Post

Replies

Boosts

Views

Activity

Why does the .png file I export from View(SwiftUI) not contain the ImageView?
This is a macOS app, while using Image("my file url") in my CoverImageView, I can export a .png file that contains the Image("my file url") view successfully. However, this doesn't happen when I use Image(nsImage: NSImage(data: imageData.imagedata)), the imageData.imagedata is a Data type that will be gotten when I tap a button and get it from a selected picture (I will show it later in my code). When I select an image, it can be seen in my app's View. However, when I save this View(CoverImageView) to a .png file it only contain the blue view, the Mac view is gone!!!! Here is the CoverImageView from which I want to create a .png file struct CoverImageView: View { @EnvironmentObject var imageData: ImageData var body: some View { ZStack { Rectangle() .frame(width: 512, height: 600) .foregroundColor(.blue) Image(nsImage: (NSImage(data: imageData.imagedata) ?? NSImage(byReferencing: URL(fileURLWithPath: "")))) .resizable() .aspectRatio(contentMode: .fit) .frame(width: 512, height: 512) } } } This is the main view PhotoTestView struct PhotoTestView: View { @State private var imageUrl: URL = URL(fileURLWithPath: "") @EnvironmentObject var imageData: ImageData var body: some View { VStack { CoverImageView() Divider() .frame(width: 1024) HStack { Button(action: { if let openURL = ImageProcess().showOpenPanel() { imageUrl = openURL if let codedImages = try? Data(contentsOf: openURL) { imageData.imagedata = codedImages } } }, label: { Image(systemName: "doc.badge.plus") }) Button(action: { ImageProcess().saveImage() }, label: { Image(systemName: "square.and.arrow.down") }) }.padding() } } } The View Extension which will create a png file from a view extension View { func imageRepresentation(rect: CGRect) -> NSBitmapImageRep? { let hosting = NSHostingView(rootView: self) hosting.setFrameSize(rect.size) hosting.setBoundsSize(rect.size) hosting.layout() hosting.layerContentsRedrawPolicy = .onSetNeedsDisplay hosting.setNeedsDisplay(rect) if let imageRepresentation = hosting.bitmapImageRepForCachingDisplay(in: rect) { hosting.cacheDisplay(in: rect, to: imageRepresentation) return imageRepresentation } return nil } func asImage(rect: CGRect) -> NSImage? { if let cgImage = imageRepresentation(rect: rect)?.cgImage { return NSImage(cgImage: cgImage, size: rect.size) } return nil } func asPngData(rect: CGRect) -> Data? { return imageRepresentation(rect: rect)?.representation(using: .png, properties: [:]) } } png File Reader and Save struct ImageProcess { func showOpenPanel() -> URL? { let openPanel = NSOpenPanel() openPanel.allowedContentTypes = [.image] openPanel.allowsMultipleSelection = false openPanel.canChooseDirectories = false openPanel.canChooseFiles = true let response = openPanel.runModal() return response == .OK ? openPanel.url : nil } func saveURL() -> URL? { let savePanel = NSSavePanel() savePanel.allowedContentTypes = [.png] savePanel.canCreateDirectories = true savePanel.isExtensionHidden = false savePanel.allowsOtherFileTypes = false savePanel.title = "Save your image" savePanel.message = "Choose a folder and a name to store your image." savePanel.nameFieldLabel = "File name:" let response = savePanel.runModal() return response == .OK ? savePanel.url : nil } func saveImage() { let view = CoverImageView().environmentObject(ImageData()) let imageData = view.asPngData(rect: CGRect.init(x: 0, y: 0, width: 1024, height: 768)) if let url = saveURL() { try? imageData!.write(to: url) } // print(imageData) } } Could you help me?
0
0
1.2k
Feb ’22
Does `TipKit` not available right now?
I want to try the new TipKit features but when I import it from project, Xcode told me that No such module 'TipKit' and I also try the following code to create a tip directly but it doesn't work and Xcode shows Cannot find type 'Tip'. What can I do to use the new feature? // MARK: Tips define here. struct bookmarkTip: Tip { }
2
0
1.9k
Jun ’23
What does the console output mean when using EventKitUI on Xcode 15.0 beta
I create a simple app to enable user add an agenda to their calendar. I create an EventEditViewController which can be used in my SwiftUI app. When I tap the save button appeared on the sheet, the agenda can be saved to calendar but comes with some Xcode console output: redactedMimicSaveEvent: could not find corresponding remoteUI user committed permanent objectID for hostTempID[x-apple-eventkit:///Alarm/t6]. initialTempID[x-apple-eventkit:///Alarm/t5] What does that mean?
0
0
486
Jun ’23
how can I discern which SwiftData object trigger the .NSManagedObjectContextDidSave notification ?
Presently, I am encountering an issue with SwiftData. For instance, I have a SwiftData class Ledger that encompasses an array of SingleTransaction, which is also a SwiftData class. Here is the question: when I save a Ledger, how can I discern that the .NSManagedObjectContextDidSave notification was triggered by saving the Ledger and not by saving a SingleTransaction? This distinction is crucial to circumvent unnecessary updates. I attempted the following syntax, but Xcode indicates that Cast from NSManagedObject to unrelated type Ledger always fails. List {...} .onReceive( NotificationCenter .default .publisher(for: .NSManagedObjectContextDidSave) .receive(on: DispatchQueue.main), perform: { notification in if let userInfo = notification.userInfo, let updatedObjects = userInfo[NSUpdatedObjectsKey] as? Set<NSManagedObject> { if updatedObjects.contains(where: { $0 is Ledger }) { fetchLedgers() } } } ) What can I do?
3
0
329
Oct ’24
Does SwiftData currently supports data sharing among multiple users through iCloud?
Currently, I am planning to add a new feature to my app that allows multiple users to collaboratively manage a single legder. Initially, I chose SwiftData with iCloud for development, so I wanted to inquire whether SwiftData currently supports data sharing among multiple users through iCloud. If it does not, should I transition entirely to Core Data, or is it feasible to allow Core Data and SwiftData to work together?
1
0
343
Oct ’24