问题
I have been trying to use the UISaveVideoAtPathToSavedPhotosAlbum to save my video (stored locally within the application). But when I try to save it, i get an error saying "Operation failed because video file is invalid and cannot be played." The file is only about a minute long and is a .mp4 file. I don't have a problem playing it with MPMoviePlayer, it just won't save. Here is the code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"videoFile" ofType:@"mp4"];
UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(status:didFinishSavingWithError:contextInfo), nil);
Is this method not designed to work for the iPad? It says "SavedPhotosAlbum". Does that mean I will have to go through the photos app to view it, or is that just the name of the method and it will be in the Videos app? If you could help me work this out, I would greatly appreciate it.
回答1:
It should work as long as UIVideoAtPathIsCompatibleWithSavedPhotosAlbum() returns true. However, I've had this issue before and seemed to have better luck creating and ALAssetsLibrary object and then using the method:
- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock
I didn't try to edit this to be generic or 100% portable. I just grabbed the code I wrote for saving videos to give you a good starting point for using ALAssetLibrary instead:
- (void) saveVideoFile:(NSString *)fullpathVideoFile completionTarget:(id)theCompletionTarget action:(SEL)theCompletionAction context:(id)theContext
{
writeFailed = NO;
completionTarget = theCompletionTarget;
completionAction = theCompletionAction;
completionContext = theContext;
// ALAssetsLibraryWriteVideoCompletionBlock
//
void (^completionBlock)(NSURL *, NSError *) = ^(NSURL *assetURL, NSError *error)
{
if ( error != nil )
{
writeFailed = YES;
}
writingToLibrary = NO;
[self notifyCompletionTarget];
};
// clean up from previous calls
//
if ( assetURL != nil )
{
[assetURL release];
assetURL = nil;
}
if ( assetFullPathName != nil )
{
[assetFullPathName release];
assetFullPathName = nil;
}
writingToLibrary = YES;
// make sure we have a good file
//
if ( [[NSFileManager defaultManager] fileExistsAtPath:fullpathVideoFile] == NO)
{
writingToLibrary = NO;
writeFailed = YES;
[self notifyCompletionTarget];
return;
}
// set assetURL for sending to the library
//
assetFullPathName = [[NSMutableString alloc] initWithCapacity:(NSUInteger)1024];
[assetFullPathName setString:fullpathVideoFile];
assetURL = [[NSURL alloc] initFileURLWithPath:assetFullPathName isDirectory:NO];
// Use possible alternative method if this method doesn't want to work
//
if ( [library videoAtPathIsCompatibleWithSavedPhotosAlbum:assetURL]==NO )
{
if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum( assetFullPathName ) )
{
UISaveVideoAtPathToSavedPhotosAlbum( assetFullPathName, self, @selector(video:didFinishSavingWithError:contextInfo:), nil );
}
else
{
writingToLibrary = NO;
writeFailed = YES;
[self notifyCompletionTarget];
}
return;
}
// Write the video to the library
//
[library writeVideoAtPathToSavedPhotosAlbum:assetURL completionBlock:completionBlock];
}
回答2:
Here's some simpler working code from one of my projects that should save a movie / video to the photo album if it exists at the filePathString below and is compatible with the photo album for the device. I'm guessing that there either was no file at the file path entered or (more likely) the movie size and format was not compatible with the photo album for the iPad / iPhone. See the question about video formats compatible with iOS device photo albums for more details.
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// don't forget to link to the AssetsLibrary framework
// and also #import <AssetsLibrary/AssetsLibrary.h>
NSString *filePathString = [[NSBundle mainBundle] pathForResource:@"videoFile" ofType:@"mp4"];
NSURL *filePathURL = [NSURL fileURLWithPath:filePathString isDirectory:NO];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:filePathURL]) {
[library writeVideoAtPathToSavedPhotosAlbum:filePathURL completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
// TODO: error handling
} else {
// TODO: success handling
}
}];
}
[library release];
来源:https://stackoverflow.com/questions/4493673/save-video-to-ipad-videos-app