iOS Swift中PHPhotoLibrary保存带文字视频报Cocoa错误-1的原因
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
imageWithTextmight not have a valid underlyingCGImage(check ifimageWithText.cgImageisnil). - 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
.movor.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
PHPhotoLibrarysave operation before yourtextToImagerendering 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

