问题:
保存我的应用中录制的视频时,如果视频大小/持续时间太大/太长,我的应用会崩溃而没有日志/异常。
我的设置:
在我的应用程序中,我使用UIImagePickerController来录制视频。现在我注意到如果我的视频长度非常长(例如使用UIImagePickerControllerQualityTypeMedium 30分钟,或使用UIImagePickerControllerQualityTypeIFrame1280x720超过一分钟),保存视频时,应用程序崩溃。有时有,有时没有警告。现在我开始调试并注意到它与内存有关(malloc_error)。
我使用分析器实时检查分配,并注意到当它要保存视频时,分配突然变得非常大(我想这与视频的临时内存使用情况有关?)在它最终崩溃之前。以下是分析器的屏幕截图:
应用必须能够录制最长1小时的视频(指定任何质量)。
我尝试了什么:
代码:
- (void)openCamera:(id)sender context:(NSManagedObjectContext*)context {
self.context = context;
//Set self as delegate (UIImagePickerControllerDelegate)
[self.picker setDelegate:self];
//If the device has a camera
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.allowsEditing = YES;
self.picker.videoQuality = [Settings videoQualitySetting];
//If the camera can record video
NSString *desired = (NSString *)kUTTypeMovie;
if ([[UIImagePickerController availableMediaTypesForSourceType:self.picker.sourceType] containsObject:desired]) {
//Let the user record video
self.picker.mediaTypes = [NSArray arrayWithObject:desired];
self.picker.videoMaximumDuration = MaxDuration;
}
else {
NSLog(@"Can't take videos with this device"); //debug
}
//Present the picker fullscreen/in popover
if ([Settings shouldDisplayFullScreenCamera]){
[self presentModalViewController:self.picker animated:YES];
[self.masterPopoverController dismissPopoverAnimated:YES];
}
else {
if (!_popover){
_popover = [[UIPopoverController alloc] initWithContentViewController:self.picker];
}
[_popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
else {
NSLog(@"Does not have a camera"); //debug
}
}
选择图像时的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
// Save the video, and close the overlay
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo) {
self.tempVideoPath = [[info objectForKey:
UIImagePickerControllerMediaURL] path];
[LocalVideoStorage saveVideo:[NSData dataWithContentsOfPath:self.tempVideoPath name:self.videoName];
[_picker dismissModalViewControllerAnimated: YES];
[[_picker parentViewController] dismissModalViewControllerAnimated:YES];
[_popover dismissPopoverAnimated:YES];
}
}
最后,当它被保存时:
+ (NSString*)saveVideo:(NSData*)video:(NSString*)videoName {
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.MOV", videoName]]; //add our video to the path
[fileManager createFileAtPath:fullPath contents:video attributes:nil]; //finally save the path (video)
NSLog(@"Video saved!");
return fullPath;
}
我正在使用ARC和iOS 5.1.1
更新 我在malloc_error_break上设置了断点,在仪器中我可以看到它来自:
# Address Category Timestamp Live Size Responsible Library Responsible Caller
0 0x10900000 Malloc 473,29 MB 02:08.951.332 • 496283648 Foundation -[NSData(NSData) initWithContentsOfFile:]
解决方案: 作为lawicko& john.k.doe说,我试图将视频从它的路径加载到NSData变量中。这导致整个视频被加载到内存中。我现在只是移动文件(& rename) copyItemAtPath
而不是这样做NSError *error = nil;
if (![fileManager copyItemAtPath:path toPath:fullPath error:&error])
NSLog(@"Error: %@", error);
答案 0 :(得分:10)
你的问题就在这一行:
[NSData dataWithContentsOfPath:self.tempVideoPath]
您显然正在尝试将此文件的内容一次性加载到内存中,但iOS绝不会让您一次加载这么多内容。您的saveVideo
方法似乎只将文件从临时位置复制到文档目录。如果这是您需要做的唯一事情,那么请查看NSFileManager的copyItemAtPath:toPath:error方法。您可以更改saveVideo
方法以将临时文件路径作为参数,而不是数据。
答案 1 :(得分:2)
为什么要从中提取媒体流的全部内容
[[info objectForKey:UIImagePickerControllerMediaURL] path]
NSData*
然后将它们写回来?你的媒体流将是巨大的!
在保存时发生这种情况的原因是因为您正在录制数据,它将转到“磁盘”,然后您将其读入内存以将其写回另一个磁盘文件。
您是否考虑过使用NSFileManager
将文件从[[info objectForKey:UIImagePickerControlMediaURL] path]
复制到您创建的名称(fullPath)?这应该避免把整个东西拉进记忆中;它应该是“文件到文件”转移。