jjzjj

ios - ALAssetsLibraryWriteVideoCompletionBlock 返回未定义的值

coder 2023-09-29 原文

ALAssetsLibraryWriteVideoCompletionBlock 正在返回未定义的值。如果assetURL等于nil,那么error应该不等于nil,也就是返回给我一些错误描述。

请参阅 here 上的苹果文档.

当我录制小视频时,ALAssetsLibraryWriteVideoCompletionBlock 返回一个很好的 assetURL 值,但是当我录制 3 或 4 Gb 的长视频时,assetURL 返回 nil,错误返回 nil。录制的视频在 tmp 文件中,因为我可以在我的应用程序的临时文件夹中看到该视频。似乎 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];
                                    }
                                }];
     }
}

最佳答案

我有几乎相同的问题:只是我尝试保存照片,而不是电影;但 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];
}

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

关于ios - ALAssetsLibraryWriteVideoCompletionBlock 返回未定义的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20813540/

有关ios - ALAssetsLibraryWriteVideoCompletionBlock 返回未定义的值的更多相关文章

随机推荐