Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7084083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:15:28+00:00 2026-05-28T07:15:28+00:00

i saw the post about using the iPhone 4/4s flash led as a torch.

  • 0

i saw the post about using the iPhone 4/4s flash led as a torch.
I found it very useful, i tried to use it for quickly turn on/off the led so to use it as a morse transmitter, but it doesn’t work.
It is too slow for this use, following the code i use:

-(void)toggleTorch
{
    AVCaptureDevice *_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    // check if the device has the torch
    if([_device hasTorch] && [_device hasFlash])
    {
        if (_device.torchMode == AVCaptureTorchModeOff) 
        {            
            // we want to turn the torch on
            AVCaptureDeviceInput *_flashInput = [AVCaptureDeviceInput deviceInputWithDevice:_device error:nil];
            AVCaptureVideoDataOutput *_output = [[AVCaptureVideoDataOutput alloc] init];

            AVCaptureSession *_session = [[AVCaptureSession alloc] init];
            [_session beginConfiguration];
            [_device lockForConfiguration:nil];

             [_session addInput:_flashInput];
             [_session addOutput:_output];

            [_device unlockForConfiguration];

            //[_output release];
            [_session commitConfiguration];
            [_session startRunning];

            [self setTorchSession:_session];
        }
        else 
        {            
            [self.torchSession stopRunning];
        }
    }
}

// turn the torch on/off
-(IBAction)toggleTorch:(id)sender
{
    AVCaptureDevice *_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    // check if the device has the torch
    if([_device hasTorch] && [_device hasFlash])
    {
        if (_device.torchMode == AVCaptureTorchModeOff) 
        {
            [self switchTorchON];
        }
        else 
        {
            [self switchTorchOFF];
        }
    }
}

-(void)switchTorchON
{
    [NSThread detachNewThreadSelector:@selector(changeSwitchImage) 
                             toTarget:self 
                           withObject:nil];

    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil) {

        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        [device lockForConfiguration:nil];

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

        [device unlockForConfiguration];

    }
}
-(void)switchTorchOFF
{
    [NSThread detachNewThreadSelector:@selector(changeSwitchImage) 
                             toTarget:self 
                           withObject:nil];
    // test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above
    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil) {

        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        [device lockForConfiguration:nil];

        [device setTorchMode:AVCaptureTorchModeOff];
        [device setFlashMode:AVCaptureFlashModeOff];

        [device unlockForConfiguration];

    }   
}

-(IBAction)toggleSOS:(id)sender
{
    // morse SOS: ...---...
    [self switchTorchON];
    [self switchTorchOFF];

    [self switchTorchON];
    [self switchTorchOFF];

    [self switchTorchON];
    [self switchTorchOFF];

}

when i push on the sos button i see just a flash.
Anyone may help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:15:28+00:00Added an answer on May 28, 2026 at 7:15 am

    The method you are using is indeed very slow. Use the method found in this Stackoverflow link instead.

    The actual on/off code is:

    [self.myDevice lockForConfiguration:nil];
    [self.myDevice setTorchMode:AVCaptureTorchModeOn];
    [self.myDevice setFlashMode:AVCaptureFlashModeOn];
    [self.myDevice unlockForConfiguration];
    

    But make sure myDevice is initialized (see the link)

    Implement it with NStimer to create a flash length of however long you want.

    EDIT:

    Sorry, I assumed that you were trying to gather the morse code input and then instructing the flash to toggle through NSTimer.

    Try using NSTimer or sleep the thread to increase the time in between the intervals of the flashing. It might be executing too fast for the actual flash component to handle. Try this on the SOS method for easiness of testing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw this post about using the new holographic themes on Honeycomb: Change theme
I saw any post about Reachability but people doesn't really give the exact answer
I saw this post: Typos… Just use option strict and explicit please.. during one
Reading a post about redis I saw this word: REPL . Can someone tell
I saw the post ( Adding song to itunes with C# sdk/api ) about
Saw this post at CodeProject for FCKEditor. Can someone explain what about the new
I saw this example about storing login information in cookie. http://eisabainyo.net/weblog/2009/02/20/store-login-information-in-cookie-using-jquery/ Tried it, but
What does the [@post, Comment.new] construction literally mean? I'm using it (saw in a
Somewhere I saw a post about an optimized way of creating a boost shared_ptr
I saw this post on Sitepoint quoting a statement by Rasmus Lerdorf which goes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.