Post

Replies

Boosts

Views

Activity

Reply to Save images in SwiftData
I am using visionOS. To your model, I am adding default as nil @Model class User { var name: String @Attribute(.externalStorage) var image: Data? var createdAt: Date init(name: String, image: Data? = nil, createdAt: Date = .now) { self.name = name self.image = image self.createdAt = createdAt } } The following code is working for me: enum ImageConversionError: Error { case imageNotFound case pngConversionFailed } func convertImageToPNG(uiImage: UIImage?) throws -> Data { guard let image = uiImage else { throw ImageConversionError.imageNotFound } guard let pngData = image.pngData() else { throw ImageConversionError.pngConversionFailed } return pngData } func createUser(name: String, uiImage: UIImage?) -> User { do { let pngData = try convertImageToPNG(uiImage: uiImage) print("Image converted to PNG data successfully.") return User(name: name, image: pngData) } catch ImageConversionError.imageNotFound { print("Error: Image not found.") } catch ImageConversionError.pngConversionFailed { print("Error: PNG conversion failed.") } catch { print("An unexpected error occurred: \(error).") } return User(name: name) }
May ’24
Reply to Store USDZ with SwiftData
I am notice something on the file that is being saved. When I try saving an usdz file like that, a file is created in some tmp foldertmp/351CD7C7-9AAD-43DE-B3C6-7E5C8A1CA70A.tmp The file has tmp at the end. If I change to .usdz I can load the file manually. But I believe we should not be doing that right?
May ’24
Reply to Store USDZ with SwiftData
I figure it out how. Basically there are no changes in the Item. The point is the way we open it. func usdzToData(named: String) -> Data? { do { createFolderForAppIfNeed() guard let usdzURL = Bundle.main.url(forResource: named, withExtension: "usdz") else { fatalError("Unable to find USDZ file in the bundle.") } /// according to https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html let fileName = UUID().uuidString guard let path = getPath()?.appendingPathComponent("\(fileName).usdz") else { print("An unexpected error occurred") return nil } print("usdzURL: \(usdzURL)") print("path: \(path)") let usdzData = try Data(contentsOf: usdzURL) try usdzData.write(to: path) return usdzData } catch { print("Error loading USDZ file: \(error)") } return nil } func urlFromData(_ data: Data, withExtension fileExtension: String = "usdz") -> URL? { do { createFolderForAppIfNeed() let tempDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first let fileName = UUID().uuidString let tempFileURL = tempDirectory!.appendingPathComponent(folderName).appendingPathComponent(fileName).appendingPathExtension(fileExtension) try data.write(to: tempFileURL) print(String(describing: tempFileURL)) return tempFileURL } catch { print(error) return nil } } Even though this is working. I would like to know if there is a better way. because it seems overkill. Since it says everywhere that SwiftData magically manage your files and so on. It would be cool If I can just do like: Model3D(from: item.usdz) Instead of Model3D(url: urlFromData(item.usdz!)!)
May ’24
Reply to Using Vision Pro in multiple rooms
Of course it is possible. Just open another application. I am not sure if you are working about the control center, or where are the apps. But if you are looking for the control center, just look up and click on the green dot that will show up. If you want all the apps to show up, you can find there also the options for the apps. And if you want to recenter all the open apps based on the position of your room, just press the Digital Crown and it will recenter the apps based on your position.
Jul ’24
Reply to Fatal error: UnsafeRawBufferPointer with negative count
I was following: https://stackoverflow.com/questions/72634317/fatal-error-unsaferawbufferpointer-with-negative-count-on-deleting-objects-fr And for me I had: var container = NSPersistentContainer(name: "Model") And I had a file called: MyModels.xcdatamodeld I need it to make both names the same. So or change the file name to Model.xcdatamodeld or the name: "Model" to name: "MyModels"
1w