
保存我的应用中记录的视频时,如果视频大小/持续时间太大/太长,我的应用程序崩溃时没有日志/异常.
我的设置:
在我的应用程序中,我使用UIImagePickerController来录制视频.现在我注意到如果我的视频长度非常长(例如使用UIImagePickerControllerQualityTypeMedium 30分钟,或使用UIImagePickerControllerQualityTypeiframe1280x720超过一分钟),保存视频时应用程序崩溃.有时有,有时没有警告.现在我开始调试并注意到它与内存有关(malloc_error).
我使用分析器来实时检查分配,并注意到,当它要保存视频时,分配突然变得非常大(我想这与视频的临时内存使用情况有关?)在它最终崩溃之前.以下是分析器的屏幕截图:
该应用必须能够录制最长1小时的视频(指定任何质量).
我尝试过的:
>将picker.vIDeoMaximumDuration设置为更短/更长
>使用分析器/仪器进行调试
>检查是否有泄漏
>关闭所有打开的应用程序&删除设备上的应用程序(用于存储清理)以获得更多内存
码:
- (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;} 我在iOS 5.1.1中使用ARC
更新:
我在malloc_error_break上放了一个断点,在仪器中我可以看到它来自:
# Address category Timestamp live Size Responsible library Responsible Caller0 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);解决方法 你的问题是这一行:
[NSData dataWithContentsOfPath:self.tempVIDeoPath]
您显然是在尝试将此文件的内容一次性加载到内存中,但iOS绝不会让您一次加载这么多内容. saveVIDeo方法似乎只将文件从临时位置复制到文档目录.如果这是您需要做的唯一事情,那么请看一下NSfileManager的copyItemAtPath:toPath:error方法.您可以更改saveVIDeo方法以将临时文件路径作为参数而不是数据.
总结以上是内存溢出为你收集整理的iphone – 保存录制的视频太长时,应用程序崩溃全部内容,希望文章能够帮你解决iphone – 保存录制的视频太长时,应用程序崩溃所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)