ALAssetsLibraryWriteVideoCompletionBlock返回未定义的值

时间:2013-12-28 10:35:45

标签: ios iphone video alassetslibrary

ALAssetsLibraryWriteVideoCompletionBlock正在返回未定义的值。如果assetURL等于nil,则错误应该不等于nil,即返回给我一些错误描述。

请参阅here上的Apple文档。

当我录制小视频时,ALAssetsLibraryWriteVideoCompletionBlock会返回一个很好的assetURL值,但是当我录制长视频3或4 Gb时,assetURL返回nil并且错误返回nil。录制的视频是在tmp文件中,因为我可以在我的应用程序中的temporally文件夹中看到此视频。似乎IOS框架尝试将此临时文件复制到相册,而iPhone没有足够的内存将此临时文件复制到相册并返回此文件的路径(assetURL)。

这是iOS框架中的错误吗?如果是这样,有没有办法解决它?

更新: 我的文件小于4GB。感谢

更新源代码:

    -(void)recorder:(AVCamRecorder *)recorder recordingDidFinishToOutputFileURL:(NSURL *)outputFileURL error:(NSError *)error
    {    
    if ([[self recorder] recordsAudio] && ![[self recorder] recordsVideo]) {
    // If the file was created on a device that doesn't support video recording, it can't be saved to the assets 
    // library. Instead, save it in the app's Documents directory, whence it can be copied from the device via
    // iTunes file sharing.
    [self copyFileToDocuments:outputFileURL];

    if ([[UIDevice currentDevice] isMultitaskingSupported]) {
        [[UIApplication sharedApplication] endBackgroundTask:[self backgroundRecordingID]];
    }       

    if ([[self delegate] respondsToSelector:@selector(captureManagerRecordingFinished:)]) {
        [[self delegate] captureManagerRecordingFinished:self];
    }
}
else {  
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
                                completionBlock:^(NSURL *assetURL, NSError *error) {
                                    if (error) {
                                        if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) {
                                            [[self delegate] captureManager:self didFailWithError:error];
                                        }                                           
                                    }

                                    if ([[UIDevice currentDevice] isMultitaskingSupported]) {
                                        [[UIApplication sharedApplication] endBackgroundTask:[self backgroundRecordingID]];
                                    }

                                    if (assetURL!=nil)
                                    {
                                        if ([[self delegate] respondsToSelector:@selector(captureManagerRecordingFinished:)]) {
                                        [[self delegate] captureManagerRecordingFinished:self];
                                        }
                                    }
                                    else
                                    {
                                        NSLog(@"Video is not saved");
                                        NSString *alertMsg = [NSString stringWithFormat:@"Impossible to copy video to photo album"];
                                        UIAlertView *alert = [[UIAlertView alloc] init];
                                        [alert setTitle:@"info"];
                                        [alert setMessage:alertMsg];
                                        [alert setDelegate:self];
                                        [alert addButtonWithTitle:@"Accept"];
                                        [alert show];
                                    }
                                }];
     }
}

2 个答案:

答案 0 :(得分:1)

我有几乎相同的问题:只是我试图保存照片,而不是电影;但仍然assetURL为NULL。这是我的代码:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
[library writeImageToSavedPhotosAlbum:(__bridge CGImageRef)([info objectForKey:UIImagePickerControllerOriginalImage])
                          orientation:ALAssetOrientationUp completionBlock:^(NSURL *assetURL, NSError *error) {
                              if(error == nil) {
                                  _myImageUrl = [NSString stringWithFormat:@"%@",assetURL];
                                  NSLog(@"%@",assetURL);
                              } else NSLog(@"%@",error);
                          }];

[picker dismissViewControllerAnimated:YES completion:nil];
}

所以,我猜这个问题与文件的大小无关。

答案 1 :(得分:0)

我做了不同的测试,我可以保存超过2Gb的视频。在这种情况下,我删除了iPhone中的很多文件,直到我有7Gb,然后我保存了一个3.2Gb(3.8Gb免费)的视频并存储在照片库中。然后我用2Gb保存了其他视频,并没有保存在照片库中。这是什么意思?。好吧,当您保存临时视频时,您至少需要与您尝试复制到照片库的视频相同或更多的可用空间。

这里的问题是IOS7框架没有返回任何错误,他的函数writeVideoAtPathToSavedPhotosAlbum,在这种情况下它必须返回ALAssetsLibraryWriteDiskSpaceError = -3305。也许在之前版本的IOS框架中它可以工作......但是在IOS7中没有。

顺便说一下,我在苹果虫记者面前打开一张票。如果他们告诉我一些不同的东西,我会发布它。

感谢所有人。

ADD:仅供参考,我在IOS 7.0.3中保存了一个超过4Gb(4.6Gb)的视频文件