iOS-app最小化时如何从相机拍照?
(即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];
}];
}
当应用程序处于活动状态时,此代码可以正常工作。但是当应用程序最小化时,请采取错误(SIGABRT)。
也许其他图书馆可以负担得起吗?
答案 0 :(得分:5)
出于隐私原因,当您的应用在后台时,您无权访问相机。
为什么?
嗯,我很高兴你这么问。故事时间!
由于鲍勃是一个非常健忘的人,他有一天忘记将手机留在工作之外的储物柜里。当他站在超级秘密的鲨鱼食谱上时,他感觉自己的手机放在口袋里,把它拉出来。它嗡嗡作响,因为他刚收到一条短信。
就在那一刻, John的秘密窃取者使用Bob的手机后置摄像头拍照,将其发送给John的服务器,而Bob从未知道。
第二天,全世界都知道控制鲨鱼的秘密项目。
这是一个极端的例子,但它是规则的主体。 Apple的政策是用户始终处于控制之中 - 以避免像Bob这样的情况。