如何让我的iPhone 4手电筒应用程序在关闭时不闪烁?

时间:2011-08-10 05:09:00

标签: iphone ios xcode

我正在玩一个简单的小手电筒应用程序,当你按下我视图上的按钮时可以打开和关闭LED闪光灯。

它工作正常,但是当我关闭闪光灯时,它会在关闭之前闪烁一次。是什么导致了这种行为?

以下是相关代码:

//
//  No_Frills_FlashlightViewController.m
//  No Frills Flashlight
//
//  Created by Terry Donaghe on 8/9/11.
//  Copyright 2011 Tilde Projects. All rights reserved.
//

#import "No_Frills_FlashlightViewController.h"

@implementation No_Frills_FlashlightViewController

@synthesize AVSession;


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)TurnOnLight:(id)sender {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    AVSession = [[AVCaptureSession alloc] init];

    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    [AVSession addInput:input];

    AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
    [AVSession addOutput:output];

    [AVSession beginConfiguration];
    [device lockForConfiguration:nil];

    [device setTorchMode:AVCaptureTorchModeOn];
    [device setFlashMode:AVCaptureFlashModeOn];

    [device unlockForConfiguration];
    [AVSession commitConfiguration];

    [AVSession startRunning];

    [self setAVSession:AVSession];    

    [output release];
}

- (IBAction)TurnOffLight:(id)sender {

    [AVSession stopRunning];
    [AVSession release];
    AVSession = nil;
}

- (IBAction)DoNothing:(id)sender {
}
@end

AVSession只是一个类级别的AVCaptureSession变量。

是的,这是我刚刚在互联网上找到的代码。我只是在玩,并试图解决问题。

1 个答案:

答案 0 :(得分:0)

我弄清楚发生了什么,它与代码无关。 :) ID10T错误。

我复制了“开启”按钮以创建“关闭”按钮。我忘了取消“关闭”按钮与由于复制而在那里的TurnOnLight方法的连接。

我只是删除了该连接,现在应用程序完美无缺! :)

经验教训:有时候你的源代码不是问题所在。 :d