You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在通过UIActivityView分享截图时为文件设置文件名

Add Filename to Shared Image in UIActivityViewController

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

  1. We convert the rendered image to data (PNG in this case, but you can switch to JPEG if needed).
  2. We create an NSItemProvider to handle the image data—this is the object that communicates with the share sheet.
  3. 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.
  4. We pass the item provider to UIActivityViewController instead 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 23:17:47