Iphone 从相机拍摄图像时闪烁的闪光灯
我正在使用AVCapture frame work设置从相机拍照时闪烁的闪光灯。在这种方法中,我得到了几秒钟的闪光灯闪烁效果,但随后它就崩溃了 下面是我已经完成的代码Iphone 从相机拍摄图像时闪烁的闪光灯,iphone,ipad,Iphone,Ipad,我正在使用AVCapture frame work设置从相机拍照时闪烁的闪光灯。在这种方法中,我得到了几秒钟的闪光灯闪烁效果,但随后它就崩溃了 下面是我已经完成的代码 -(IBAction) a { _picker = [[UIImagePickerController alloc] init]; _picker.sourceType = UIImagePickerControllerSourceTypeCamera; _picker.cameraCaptureMode
-(IBAction) a
{
_picker = [[UIImagePickerController alloc] init];
_picker.sourceType = UIImagePickerControllerSourceTypeCamera;
_picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
_picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
_picker.showsCameraControls = YES;
_picker.navigationBarHidden =YES;
_picker.toolbarHidden = YES;
_picker.wantsFullScreenLayout = YES;
[_picker takePicture];
// Insert the overlay
OverlayViewController *overlay = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
overlay.pickerReference = _picker;
_picker.cameraOverlayView = overlay.view;
_picker.delegate = (id)self;
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(flashLight_On_Off) userInfo:nil repeats:YES];
[self presentModalViewController:_picker animated:NO];
}
- (void)flashLight_On_Off
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices)
{
if ([device hasFlash] == YES)
{
[device lockForConfiguration:nil];
if (bulPicker == FALSE)
{
[device setTorchMode:AVCaptureTorchModeOn];
bulPicker = TRUE;
}
else
{
[device setTorchMode:AVCaptureTorchModeOff];
bulPicker = FALSE;
}
[device unlockForConfiguration];
}
}
}
有什么问题吗?有没有其他方法可以解决这个问题?在按下“使用”按钮之前,我还必须在拍照后停止闪烁
请给我建议合适的解决方案 如果我正确地回忆起Cocoa命名约定,则除
-alloc
、-copy
和-mutableCopy
之外的任何方法都会返回自动删除的对象。在这种情况下,
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
每秒调用10次,每次自动释放。这意味着它可能不会立即发布,因此它将开始消耗你的RAM,操作系统最终会检测到这一点并终止你的应用程序进程
您应该做的是将这些类型的操作包装到自动释放池中,前提是您事先知道它们会被大量调用
- (void)toggleFlashlight
{
@autoreleasepool {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices)
{
if ([device hasFlash]) {
[device lockForConfiguration:nil];
if (bulPicker) {
[device setTorchMode:AVCaptureTorchModeOff];
bulPicker = NO;
} else {
[device setTorchMode:AVCaptureTorchModeOn];
bulPicker = YES;
}
[device unlockForConfiguration];
}
}
}
}
此外,在Objective-C中,您应该使用
YES
和NO
而不是TRUE
和FALSE
,并且您不应该将布尔运算的结果与YES
(最多与NO
)进行比较。我在XCode 4.5和iOS 6.0中使用ARC概念,因此我不能使用“release”关键字anywhere.@JohnPatel autorelease是一个有效的概念-弧或无弧:)@H2CO3谢谢…这工作正常…有时它不会打开快门窗口拍照。。。单击“图像捕获”按钮后,我无权保持lightON几秒钟。@H2CO3是否有解决此问题的选项。。?如果我将更改代码内的属性以访问代码,苹果会拒绝应用程序吗?