捕获照片如何最小化iOS-app?

Sin*_*nba 3 cocoa-touch objective-c avfoundation ios avcapturesession

iOS-app最小化时如何从相机拍照?
(即applicationDidEnterBackground:/ 之后applicationWillResignActive:)

AppDelegate.m: (感谢你链接)

- (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;
}
Run Code Online (Sandbox Code Playgroud)

OurViewController.m: (感谢你链接)

-(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];
     }];
}
Run Code Online (Sandbox Code Playgroud)

当应用程序处于活动状态时,此代码可正常工 但是当应用程序最小化时,请采取错误(SIGABRT).

也许还有其他图书馆能负担得起吗?

Und*_*ndo 5

出于隐私原因,当您的应用在后台时,您无权访问相机.

为什么?

好吧,我很高兴你这么问.讲故事的时间!


鲍勃是一个在国家安全局工作的人,正在开发超级秘密控制鲨鱼的猴子.为什么?他不能说.

Bob有一天将一个应用程序下载到他的iPhone上,名为John's Secret Stealer.鲍勃不读应用程序标题.

由于鲍勃是一个非常健忘的人,他有一天忘了把手机留在工作之外的储物柜里.当他站在超级秘密的鲨鱼食谱上时,他感觉自己的手机放在口袋里,把它拉出来.它嗡嗡作响,因为他刚收到一个短信.

就在那一刻,约翰的秘密盗窃者用鲍勃的手机后置摄像头拍了一张照片,把它送到约翰的服务器上,鲍勃从来不知道.

第二天,整个世界都知道控制鲨鱼的秘密项目.


这是一个极端的例子,但它是规则的主体.Apple的政策是用户始终处于控制之中 - 以避免像Bob这样的情况.