私はあなたのための回避策を見つけたかもしれません。を試しましたAVAssetExportSession
か?
以下のサンプルでは、画面に 2 つのボタンを持つ単純なアプリを作成しました。1 つは を呼び出しますonSaveBtn:
。これは、アプリのリソース バンドルにあるビデオの URL を取得し、それをユーザーの保存済み写真アルバムに保存するだけです。(ただし、私の場合、私の動画はYES
から返されvideoAtPathIsCompatibleWithSavedPhotosAlbum:
ます。それ以外の方法で返されない動画はありませんでした。)
2 番目のボタンは に接続されてonExportBtn:
おり、保存するビデオを取得して を作成しAVAssetExportSession
、ビデオを一時ディレクトリにエクスポートしてから、エクスポートしたビデオを保存済みの写真アルバムにコピーします。エクスポートに時間がかかるため、この方法は単純なコピーよりも時間がかかりますが、これは別の方法である可能性があります - の結果を確認し、videoAtPathIsCompatibleWithSavedPhotosAlbum:
場合YES
はアルバムに直接コピーしてください。それ以外の場合は、ビデオをエクスポートしてからコピーします。
互換性コールに戻らないビデオ ファイルがなければ、NO
これがうまくいくとは 100% 確信が持てませんが、試してみる価値はあります。
また、この質問 を確認することもできます。この質問では、使用している可能性のあるデバイスで互換性のあるビデオ フォーマットを調べています。
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
- (IBAction)onSaveBtn:(id)sender
{
NSURL *srcURL = [[NSBundle mainBundle] URLForResource:@"WP_20121214_001" withExtension:@"mp4"];
[self saveToCameraRoll:srcURL];
}
- (IBAction)onExportBtn:(id)sender
{
NSURL *srcURL = [[NSBundle mainBundle] URLForResource:@"WP_20121214_001" withExtension:@"mp4"];
AVAsset *srcAsset = [AVAsset assetWithURL:srcURL];
// create an export session
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:srcAsset presetName:AVAssetExportPresetHighestQuality];
// Export the file to a tmp dir
NSString *fileName = [srcURL lastPathComponent];
NSString *tmpDir = NSTemporaryDirectory();
NSURL *tmpURL = [NSURL fileURLWithPath:[tmpDir stringByAppendingPathComponent:fileName]];
exportSession.outputURL = tmpURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
// now copy the tmp file to the camera roll
switch ([exportSession status]) {
case AVAssetExportSessionStatusFailed:
NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"Export successful");
[self saveToCameraRoll:exportSession.outputURL];
break;
default:
break;
}
}];
}
- (void) saveToCameraRoll:(NSURL *)srcURL
{
NSLog(@"srcURL: %@", srcURL);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
ALAssetsLibraryWriteVideoCompletionBlock videoWriteCompletionBlock =
^(NSURL *newURL, NSError *error) {
if (error) {
NSLog( @"Error writing image with metadata to Photo Library: %@", error );
} else {
NSLog( @"Wrote image with metadata to Photo Library %@", newURL.absoluteString);
}
};
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:srcURL])
{
[library writeVideoAtPathToSavedPhotosAlbum:srcURL
completionBlock:videoWriteCompletionBlock];
}
}