如何在通过UIActivityView分享截图时为文件设置文件名
Hey there! I totally get wanting that polished touch of showing a filename next to your shared calculation result—small details like that make the app feel way more professional. Let’s fix this with a straightforward solution that plays nice with iOS’s share system.
The Problem Right Now
Your current code only passes the rendered image directly to activityItems, so the system has no idea what filename to display. We need to wrap the image in an object that tells the share sheet exactly what name to use.
The Solution
We’ll use NSItemProvider to package the image with a custom filename. This ensures the share sheet shows the name alongside the image, and works reliably across all share destinations.
First, add this import at the top of your file (we need it for file type identifiers):
import MobileCoreServices
Then update your shareTapped method with this code:
@objc func shareTapped() { let renderer = UIGraphicsImageRenderer(size: view.bounds.size) let image = renderer.image { ctx in view.drawHierarchy(in: view.bounds, afterScreenUpdates: true) } // Convert image to PNG data (use jpegData(compressionQuality:) if you prefer JPEG) guard let imageData = image.pngData() else { print("Failed to convert image to data") return } // Create an item provider to package our image with a filename let itemProvider = NSItemProvider(item: imageData as NSSecureCoding, typeIdentifier: kUTTypePNG as String) // Register a file representation that includes our custom filename itemProvider.registerFileRepresentation(forTypeIdentifier: kUTTypePNG as String, fileOptions: []) { completionHandler in // Create a temporary file path with our desired filename let tempFilename = "Calculation_Result.png" // Customize this to whatever you want! let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(tempFilename) do { // Write the image data to the temporary file try imageData.write(to: tempURL) // Pass the file URL to the share sheet completionHandler(tempURL, nil, nil) } catch { completionHandler(nil, error, nil) } return nil } // Pass the item provider to the activity view controller let vc = UIActivityViewController(activityItems: [itemProvider], applicationActivities: []) vc.excludedActivityTypes = [.assignToContact, .addToReadingList] vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem present(vc, animated: true) }
How It Works
- We convert the rendered image to data (PNG in this case, but you can switch to JPEG if needed).
- We create an
NSItemProviderto handle the image data—this is the object that communicates with the share sheet. - We register a file representation that saves the image to a temporary file with your custom filename. The share sheet uses this filename to display next to the image icon.
- We pass the item provider to
UIActivityViewControllerinstead of the raw image.
Quick Bug Fix Note
I spotted a typo in your viewDidLoad method:
widthSizeTransfer.text = witdhSizeRelay // "witdh" is misspelled!
Double-check that variable name to make sure your width label gets populated correctly (I think you meant largeSizeRelay based on your variable declarations).
内容的提问来源于stack exchange,提问作者RobbB

