How take picture from camera when iOS-app is minimized?
(i.e. after applicationDidEnterBackground:
/ applicationWillResignActive:
)
当ios应用程序最小化时,如何从相机上拍照?(即申请后背景:/申请将会生效:)
AppDelegate.m: (thank you link)
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;
}
OurViewController.m: (thank you link)
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];
}];
}
This code works fine, when application is active. But take error (SIGABRT), when app is minimized.
当应用程序处于活动状态时,此代码可以正常工作。但当应用程序最小化时,请考虑误差(SIGABRT)。
Maybe are there other libraries can afford to do it?
也许还有其他的图书馆能负担得起吗?
1 个解决方案
#1
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.
有一天,鲍勃在他的iPhone上下载了一款名为“约翰的秘密窃贼”的应用。Bob不读应用标题。
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.
这是一个极端的例子,但这是规则的原则。苹果的政策是,用户总是处于控制之中——以避免像鲍勃那样的情况。
#1
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.
有一天,鲍勃在他的iPhone上下载了一款名为“约翰的秘密窃贼”的应用。Bob不读应用标题。
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.
这是一个极端的例子,但这是规则的原则。苹果的政策是,用户总是处于控制之中——以避免像鲍勃那样的情况。