我一直在尝试使用UISaveVideoAtPathToSavedPhotosAlbum
来保存我的视频(在应用程序中本地存储)。但是当我尝试保存它时,我收到一条错误消息“操作失败,因为视频文件无效且无法播放。”该文件只有大约一分钟的长度,是一个.mp4文件。我用MPMoviePlayer播放它没有问题,它只是不会保存。这是代码:
NSString *path = [[NSBundle mainBundle] pathForResource:@"videoFile" ofType:@"mp4"];
UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(status:didFinishSavingWithError:contextInfo), nil);
这种方法不适用于iPad吗?它说“SavedPhotosAlbum”。这是否意味着我将不得不通过照片应用程序来查看它,或者只是方法的名称,它将在视频应用程序中?如果你能帮我解决这个问题,我将非常感激。
答案 0 :(得分:4)
这是我的一个项目中的一些更简单的工作代码,如果它存在于下面的filePathString
并且与设备的相册兼容,则应将电影/视频保存到相册。我猜测输入的文件路径中没有文件,或者(更有可能)电影尺寸和格式与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];
答案 1 :(得分:1)
只要UIVideoAtPathIsCompatibleWithSavedPhotosAlbum()返回true,它就应该有效。但是,我之前遇到过这个问题,似乎有更好的运气创建和ALAssetsLibrary对象,然后使用该方法:
- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock
我没有尝试将其编辑为通用或100%便携式。我刚刚抓住了为保存视频而编写的代码,为您提供了使用ALAssetLibrary的良好起点:
- (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];
}