How to avoid duplicate video files when using PHAssetChangeRequest.FromVideo
I am writing an application importing a video file from a remote server (drone) over HTTP. After uploading the file to local storage ( /Documents/video
), I import it to my custom album PHPhotLibrary
using PHAssetChangeRequest.FromVideo
.
I can import a video, and when I access the Photos app in the simulator, the video appears in the correct album. However, if I remove it from the album, the file is not removed from my apps folder /Documents/video
.
Then I tried to delete the file from /Documents/video
after importing with PHAssetChangeRequest.FromVideo
, but then the video disappears from the Photos app.
How can we ensure that there is only one copy of a video file, preferably stored in a shared photo gallery? Can I detect that the file has been deleted so I can delete it from the folder /Documents/video
? Or do I need to "sync" this with myself every time the app is launched?
source to share
I think you should save the uploaded video to a temporary directory like
NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mov"];
And then you can save it in the Photos app.
The advantage of storing it in a temporary directory is that the OS will automatically clean it up when needed, but you can manually delete it whenever you want by creating the following class method
+ (void)clearTemporaryDirectoryData
{
NSArray* tempDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
for (NSString *file in tempDirectory) {
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
}
}
In your case, you can delete the temporary data after saving the downloaded video to the Photos app.
source to share