Share Images With Transferable Without Keeping Every Byte In Memory
Your photo editor has already written a large PNG to disk. The Share button reads it into Data, wraps that data in another model, and builds a full-resolution preview. Sharing now needs extra memory before the receiving app has asked for anything.
If the exported artifact is already a file, let the transfer representation remain a file.
Describe the artifact, not its bytes
Apple recommends FileRepresentation for large file-backed transfers. Use a specific content type so receiving apps know what they are being offered.
This example assumes an app-owned, successfully encoded PNG. Checking that a path is a regular file does not validate its image format.
import CoreTransferable
import Foundation
import SwiftUI
import UniformTypeIdentifiers
struct ExportedPNG: Transferable {
let file: URL
static var transferRepresentation: some TransferRepresentation {
FileRepresentation(exportedContentType: .png) { item in
try item.transferFile()
}
}
func transferFile() throws -> SentTransferredFile {
guard file.isFileURL else { throw ExportError.notLocal }
var checkedFile = file
checkedFile.removeAllCachedResourceValues()
let values = try checkedFile.resourceValues(forKeys: [.isRegularFileKey])
guard values.isRegularFile == true else { throw ExportError.notRegular }
return SentTransferredFile(file, allowAccessingOriginalFile: false)
}
enum ExportError: Error { case notLocal, notRegular }
}
struct ExportButton: View {
let exported: ExportedPNG
var body: some View {
ShareLink(
item: exported,
preview: SharePreview("Exported image", image: Image(systemName: "photo")))
}
}
The item contains a URL rather than a second copy of the image bytes. The preview is intentionally lightweight. A real app can use its existing small thumbnail instead of decoding the original again just to populate the share sheet.
Be precise about what access is allowed
The export initializer defaults to disallowing opening the original in place. The SentTransferredFile value also keeps original-file access disabled here.
The receiver gets a copy rather than an invitation to edit the editor’s source artifact. This is a sharing flow, so that is the desired contract. A document collaboration or open-in-place workflow would need a different ownership and coordination design.
Declaring .png does not convert a JPEG into PNG. Match the representation to the actual encoder output and file extension. If the app offers different export formats, construct the appropriate transferable type after that choice is resolved.
Refresh the check, then preserve ownership
The helper clears cached URL resource values before checking the file. Our first fixture found a subtle failure: reading metadata, deleting the file, and asking the same URL for metadata again could return the cached regular-file result. Removing cached resource values makes the new check consult current file information.
That still does not reserve the file. Another part of the app could delete it after the check and before the system reads it. Correct lifetime ownership matters more than a preflight check.
Keep the exported artifact immutable while it is available for sharing. Give each export a unique destination rather than overwriting latest.png underneath an active transfer. Otherwise, a second export can change what the first Share button sends.
Do not delete the file when the closure returns
The representation closure returns a description of a file. Its return is not proof that every receiver has finished reading it.
Avoid a defer that removes the export inside transferFile(). Also avoid tying deletion only to the SwiftUI button disappearing. A share sheet can outlive the view that created its item, and the representation may be requested more than once.
Our temporary-file article discusses ownership and cleanup boundaries. For sharing, use an export store that retains active artifacts and later removes unreferenced exports under a deliberate retention policy. A disk quota and recovery on the next launch are more useful than assuming every interaction ends with a tidy callback.
Test the receiving workflow
The local fixture verifies regular-file rejection, missing-file behavior, and the copy-only transfer description. Its tiny byte fixture deliberately does not claim to be a valid PNG.
For app integration, export an actual large image and share it to at least two relevant destinations. Verify the resulting file’s type, dimensions, and content. Cancel once, share the same item again, and trigger a second export while the first item’s share sheet remains open.
Measure memory through the complete operation, including image encoding and preview creation. A file representation removes an unnecessary app-side byte conversion; it does not promise zero copying or a fixed memory footprint inside every system component.
September 2026 review: the representation and ShareLink compile with Swift 6.4, and the standalone file checks passed. Share-sheet delivery, encoder correctness, and peak device memory remain integration measurements.