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

iOS Swift中PHPhotoLibrary保存带文字视频报Cocoa错误-1的原因

Troubleshooting Cocoa Error -1 When Saving Text-Overlaid Image Video to Photos Library

Hey there, let's break down why you're hitting that frustrating Cocoa error -1 when saving your text-overlaid image video to the Photos app. This error in the context of PHPhotoLibrary typically points to a general invalid operation, and it's almost always linked to issues with your processed media or how you're preparing it for saving. Here are the most likely culprits:

1. Corrupted or Incompatible Image Data After Text Rendering

Your textToImage method might be producing a UIImage that's either damaged or doesn't meet Photos Library's format requirements:

  • Color Space Mismatch: If you're rendering text into an image using a CMYK color space instead of RGB (the standard for Photos), the library will reject it.
  • Missing CGImage Backing: The generated imageWithText might not have a valid underlying CGImage (check if imageWithText.cgImage is nil).
  • Invalid Image Dimensions: Extremely large or non-standard image sizes can cause processing failures.

Quick validation check you can run right after generating imageWithText:

guard let imageData = imageWithText.jpegData(compressionQuality: 0.8) else {
    print("Error: Generated image has invalid data")
    return
}
// If this passes, your image data is at least structurally sound

2. Misconfigured Video Composition/Export

When converting your modified image to a video, small mistakes in the video setup can trigger this error:

  • Unsupported Export Preset: Using a non-standard export preset (instead of something like AVAssetExportPresetHighestQuality) can produce a video the Photos Library can't parse.
  • Invalid Container Format: Saving the video with an unsupported file extension (not .mov or .mp4) will cause rejection.
  • Broken Asset Tracks: If your video composition is missing required tracks (like a video track with valid timing), the save operation will fail silently with error -1.

3. Race Conditions or Authorization Issues

Even if your base code works, modifying the image can introduce asynchronous timing problems:

  • Unfinished Media Preparation: If you kick off the PHPhotoLibrary save operation before your textToImage rendering is fully complete, you're trying to save incomplete data.
  • Stale Authorization Status: Rarely, the Photos authorization might have changed (e.g., user revoked and re-enabled it mid-session) without your code checking it again before saving.

Fix: Always re-verify authorization status right before the save call, and ensure all image processing finishes synchronously before initiating the library save.

4. Memory Overload During Processing

Large original images combined with text rendering can spike memory usage, leading to corrupted video output:

  • If your source image is 4K+ resolution, rendering text on it without scaling first can cause memory pressure, which might corrupt the video data being passed to PHPhotoLibrary.

Fix: Downscale your original image to a reasonable size (e.g., match the device's screen resolution) before adding text.


内容的提问来源于stack exchange,提问作者Alex Stone

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:08:02