3

How take picture from camera when iOS-app is minimized?
(i.e. after applicationDidEnterBackground: / applicationWillResignActive: )

AppDelegate.m: (thank you link)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    //To make the code block asynchronous
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        //### background task starts
        NSLog(@"Running in the background\n");
        while(TRUE)
        {
            printf("Called"); //////Work fine

            [self.window.rootViewController captureNow]; /////Capture picture!

            [NSThread sleepForTimeInterval: 10.0]; //wait for 10 sec
        }
    });

    return YES;
}

OurViewController.m: (thank you link)

-(IBAction)captureNow {

    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in _stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection)
        {
            break;
        }
    }

    NSLog(@"about to request a capture from: %@", _stillImageOutput);
    [_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);

         if (error)
         {
             NSLog(@"ERROR = %@", error); ///// Error!
         }

         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; ////SIGABRT, cause imageSampleBuffer is nil
         UIImage *image = [[UIImage alloc] initWithData:imageData];

         UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

         [image release];
     }];
}

This code works fine, when application is active. But take error (SIGABRT), when app is minimized.

Maybe are there other libraries can afford to do it?

Community
  • 1
  • 1
Sinba
  • 314
  • 3
  • 12
  • You're not allowed access to the camera while in the background. There's probably a way around for jailbreak apps using private APIs, but you'll never get app store approval. – Ben Swanson Jul 16 '13 at 00:51
  • http://stackoverflow.com/questions/11090184/how-does-the-ios-app-display-recorder-record-the-screen-without-using-private-ap – vikingosegundo Jul 16 '13 at 02:09

1 Answers1

5

For privacy reasons, you're not allowed to access the camera when your app is in the background.

Why?

Well, I'm glad you asked that. Story time!


Bob is a person who works at the NSA, developing super-secret monkey controlling sharks. Why? He can't say.

Bob one day downloaded an app onto his iPhone, called John's Secret Stealer. Bob doesn't read app titles.

Since Bob is a very forgetful person, he one day forgot to leave his phone in the lockers outside of work. While standing over the super-secret shark recipe, he felt his phone in his pocket, and pulled it out. It had buzzed because he just got a text.

At that moment, John's Secret Stealer took a picture using Bob's phone's rear camera, sent it off to John's servers, and Bob never knew.

The next day, the entire world knew about the secret project to control sharks.


That's an extreme example, but it's the principal of the rule. Apple's policy is that the user is always in control - to avoid situations like Bob's.

Undo
  • 25,519
  • 37
  • 106
  • 129