Comments (6)
Hi,
Saving an image from a Mac app is not as easy as from an iOS app due to differences between NSImage & UIImage. UIImage is not available in a Mac app.
Here is a function I put together to save an image to a sub-folder in the Downloads folder:
func saveImage() {
let imageName = "App_Image"
let saveName = "ImageTest" + ".jpg"
// using Downloads directory as it is possible to configure an app with permission to access to Downloads
let fileManager = FileManager.default
guard
let downloadsFolder = try? fileManager.url(
for: .downloadsDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
else {
print("No Downloads folder")
return
}
// use this if you need to create a sub-folder inside the Downloads folder
let subFolder = downloadsFolder.appendingPathComponent("Test Folder")
var isDir: ObjCBool = false
if !fileManager.fileExists(atPath: subFolder.path, isDirectory: &isDir) {
do {
try fileManager.createDirectory(
at: subFolder, withIntermediateDirectories: true, attributes: nil)
} catch {
print("Error creating sub-folder: \(error.localizedDescription)")
return
}
}
let fileUrlWithName = subFolder.appendingPathComponent(saveName)
// create NSImage & get its image representation
guard let image = NSImage(named: imageName),
let imageRep = NSBitmapImageRep(data: image.tiffRepresentation!)
else {
print("No image or imageRep")
return
}
// set the image properties (JPEG compression here but there are others) & convert image represention to Data
let imageProps = [NSBitmapImageRep.PropertyKey.compressionFactor: 0.5]
guard let imageData = imageRep.representation(using: .jpeg, properties: imageProps) else {
print("No image data")
return
}
do {
// save the image data to the file
try imageData.write(to: fileUrlWithName)
} catch {
print("** saveImageData error: \(error.localizedDescription)")
}
}
In the Signing & Capabilities section of the target settings, you can set the app to have Read/Write access to the Downloads folder. If you want to save to the Desktop or to most other folders, you will have to turn on Read/Write access for "User Selected File" and use an NSSavePanel to allow the user to select where to save the file themselves.
from swiftui-mac.
Thanks, your code works fine for saving the image or other in the download
folder, but I want to save the image or other in a specific folder on the desktop and I can't use NSSavePanel
, because saving is in a loop (type for ... in
) to save items in user-defined folders.
How can I use NSSavePanel
without seeing the dialog, but only to set the URL where the image or others are to be saved?
Is it possible?
from swiftui-mac.
If you want to save to the desktop without asking the user to save manually, you could turn off sand-boxing for the app. This will mean it cannot be distributed in the App Store, but that might be OK for you.
If you do need to distribute the app through the store, then you need to ask the user to select the folder once per app launch. If you turn on Read/Write access for "User Selected File", you can use an NSOpenPanel
to ask them to select a folder which will give you write access to that folder until the app quits. Then you will be able to make sub-folders and save files anywhere inside that user selected folder.
from swiftui-mac.
As you suggested, I have disabled sand-boxing for the app and now it works (see screen shot below).
The path is saved in CoreData from this view (see screen shot below).
Would it be possibile to set App Sandbox
back to YES
and at the same time being able to save images in the folder previously set in CoreData? Whether it wouldn't be possibile, how can I distribute my app, which works both in macOS (via Catalyst) and iPad, in the event that a friend of mine asks for it?
Thanks in advance.
from swiftui-mac.
You can distribute any Mac app without using the Mac App Store. I suggest signing it with your Developer ID, but you can distribute manually or through any other channel. Archive the app as usual and then in the Organiser, when you click "Distribute App" you will be able to choose this option.
I did not realise this was a Catalyst app and I have not made one of those yet, so I am not sure how it works with regard to such permissions. On the iPad, you do need to distribute through the App Store so there is no way to turn off sand-boxing. But you can leave files in your app's own sand-boxed area. If you want these files to be accessible outside the app, then I suggest you look into saving them to the Files app.
from swiftui-mac.
Thanks so much for your clear explanations, but I don't found Organiser
and Distribute App
, in Xcode 11: Where can I find them?
For now, text my app on Mac, compiled with Xcode 11.4, extracting it from the sidebar (see screenshot below), but I don't know if this is the right procedure.
I tried to use the code on iPad, without being able to load the server, to check the possibility of saving the images in it, as for the application on macOS with Catalyst, so I can't tell you what happens if I disable AppSandbox on iPad.
Last question. Do you know how, with Xcode 11, I can set the icons for the two types of files created by my app? For the moment, the result I get is this, but I would like the two types of files to have a specific icon (I show you the icon of the app and that of one of the two types of files).
from swiftui-mac.
Related Issues (2)
- EmbeddedColorWell HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftui-mac.