Skip to content

How to make NSImage Sendable in Swift #989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
onmyway133 opened this issue Apr 22, 2025 · 0 comments
Open

How to make NSImage Sendable in Swift #989

onmyway133 opened this issue Apr 22, 2025 · 0 comments
Labels

Comments

@onmyway133
Copy link
Owner

onmyway133 commented Apr 22, 2025

In Swift 6, Apple introduced stricter rules to help make your code safer when using concurrency (like async, await, Task, and actor). These rules check that types used in concurrent code are safe to share across threads.

Types that are safe to use across threads are called Sendable.

But here’s the catch: NSImage is not Sendable by default.
That means if you try to use an NSImage inside a Task or actor, Swift will show a warning or error.

Use unchecked

If you’re sure that you’re using the image safely (e.g., you only read from it, or you handle all thread access carefully), you can force Swift to treat it as Sendable:

extension NSImage: @unchecked @retroactive Sendable {}

Use alternative Sendable friendly type

If you want to pass an image between tasks safely, convert it into something that is Sendable:

if let tiffData = image.tiffRepresentation,
   let bitmap = NSBitmapImageRep(data: tiffData),
   let pngData = bitmap.representation(using: .png, properties: [:]) {
    // pngData is Sendable ✅
}

CGImage is also a thread-safe image format you can use:

if let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) {
    // cgImage is Sendable ✅
}

Use an actor to Protect the Image

If you want to store and use NSImage safely, you can wrap it in an actor. An actor keeps everything thread-safe:

actor ImageStore {
    private var image: NSImage?

    func set(_ newImage: NSImage) {
        image = newImage
    }

    func get() -> NSImage? {
        return image
    }
}
@onmyway133 onmyway133 changed the title How to Make NSImage Sendable in Swift How to make NSImage Sendable in Swift Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant