Post

Replies

Boosts

Views

Activity

Reply to SwiftUI 2.0: export group of images with .fileExporter modifier
Hi! I posted an answer to this in your Stack Overflow question, duplicating here in case someone finds this before SO: You need to use fileExporter with documents argument instead of document, which takes in a Collection macOS example: import SwiftUI import UniformTypeIdentifiers struct ImageDocument: FileDocument { 	static var readableContentTypes: [UTType] { [.jpeg, .png, .tiff] } 	var image: NSImage 	init(image: NSImage?) { 		self.image = image ?? NSImage() 	} 	init(configuration: ReadConfiguration) throws { 		guard let data = configuration.file.regularFileContents, 					let image = NSImage(data: data) 		else { 			throw CocoaError(.fileReadCorruptFile) 		} 		self.image = image 	} 	func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper { 		/* You can replace tiff representation with what you want to export */ 		return FileWrapper(regularFileWithContents: image.tiffRepresentation!) 	} } @main struct FocalApp: App { 	@StateObject var appContext = AppContext() 	var body: some Scene { 		WindowGroup { 			MainView() 				.environmentObject(self.appContext) 				.fileExporter( 					isPresented: $appContext.fileSaveDialogShown, 					documents: [ 						ImageDocument(image: NSImage(named: "testimage1")), 						ImageDocument(image: NSImage(named: "testimage2")) 					], 					contentType: .tiff /* Match this to your representation in ImageDocument */ 				) { url in 					print("Saved to", url) /* [URL] */ 				} 		} 	} }
Feb ’21