Sunday, August 6, 2017

How to avoid duplicate video files when using PHAssetChangeRequest.FromVideo

Leave a Comment

I'm writing an app importing a video file from a remote server (drone) over HTTP. After downloading the file to local storage (/Documents/video) I import it to a custom album in the PHPhotLibrary using PHAssetChangeRequest.FromVideo.

I'm able to import the video, and when accessing the Photos app in the simulator the video appears in the correct album. However, if I delete it from the album the file is not deleted from my the apps /Documents/video folder.

I then tried to delete the file from /Documents/video after importing it using PHAssetChangeRequest.FromVideo, but then the video disappears from the Photos app.

How do I ensure there is only one copy of the video file, preferably stored in the shared photo gallery? Can I detect that a file is deleted, so that I can delete it from the /Documents/video folder? Or do I need to "sync" this my self everytime the app starts?

1 Answers

Answers 1

I think you should store the downloaded video to a temporary directory like

NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mov"];

And then you can store it into Photos app.

The benefit of storing it into temporary directory is OS will automatically clear it when required however you can manually delete it whenever you want by creating 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 temp data after saving the downloaded video into Photos app.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment