首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于AVFoundation:为什么我的应用程序第一次工作

关于AVFoundation:为什么我的应用程序第一次工作
EN

Stack Overflow用户
提问于 2014-01-23 14:43:43
回答 2查看 207关注 0票数 0

我在测试一个简单的代码。它使用AVFoundation。当我运行它的时候。它可以拍摄一张照片并保存到相册中。但当我运行两次,它就不起作用了。我重建了它然后又开始工作了。但就一次。谁能告诉我为什么吗?(对不起。我不擅长english....><)

代码语言:javascript
复制
//
//  main.m
//  test
//
//  Created by John Suu on 1/22/14.
//  Copyright (c) 2014 John Suu. All rights reserved.
//

#import <UIKit/UIKit.h>

#import "AppDelegate.h"
#import "AVFoundation/AVFoundation.h"

AVCaptureSession *session;
AVCaptureDevice *videoDevice;
AVCaptureDeviceInput *videoInput;
AVCaptureStillImageOutput *stillImageOutput;

void settings(){

    NSError *error = nil;

    // 入力と出力からキャプチャーセッションを作成
    session = [[AVCaptureSession alloc] init];

    // 正面に配置されているカメラを取得
    AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    // カメラからの入力を作成し、セッションに追加
    videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error];
    [session addInput:videoInput];

    // 画像への出力を作成し、セッションに追加
    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    [session addOutput:stillImageOutput];

    // キャプチャーセッションから入力のプレビュー表示を作成

    // セッション開始
    [session startRunning];
}

void takePicture(){

    AVCaptureConnection *videoConnection = [stillImageOutput    connectionWithMediaType:AVMediaTypeVideo];

    /*if (videoConnection == nil) {
        return;
    }*/

    // ビデオ入力から画像を非同期で取得。ブロックで定義されている処理が呼び出され、画像データを引数から取得する
    [stillImageOutput
     captureStillImageAsynchronouslyFromConnection:videoConnection
     completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
         if (imageDataSampleBuffer == NULL) {
             return;
         }

         // 入力された画像データからJPEGフォーマットとしてデータを取得
         NSData *imageData = [AVCaptureStillImageOutput   jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

         // JPEGデータからUIImageを作成
         //[imageData writeToFile:@"/var/mobile" atomically:NO];
         UIImage *image = [[UIImage alloc] initWithData:imageData];

         // アルバムに画像を保存


         UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);


     }];


}

int main(int argc, char * argv[])
{

    settings();
    takePicture();
   // NSTimer *timer =
    [NSTimer scheduledTimerWithTimeInterval:3 target:nil selector:nil userInfo:nil repeats:NO];
    NSLog(@"runing.it works!\n");


    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
  }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-24 09:25:09

您的应用程序支持回退吗?看看您的代码,这可能是原因。

默认情况下,当您退出应用程序时,它将以后台模式移动。当您再次启动它时,它将从背景移动到前景。您的main函数将不再被调用,因为后台模式的应用程序刚刚被挂起。这意味着当你从零开始启动应用程序时,takePicture函数只会被调用一次。试着关闭你的应用程序(双击主页),然后再启动它。

要解决这个问题,只需将获取图像的代码移动到AppDelegate方法中即可。如果你不知道,哪一个,读这个ref/doc/uid/TP40007072-CH4-SW3

票数 0
EN

Stack Overflow用户

发布于 2014-01-23 14:52:54

在再次打开应用程序之前,尝试一下addingAdd,一个停止AVCaptureSession的函数。

代码语言:javascript
复制
    int main(int argc, char * argv[])
    {

        settings();
        takePicture();
        stopSession();
       // NSTimer *timer =
        [NSTimer scheduledTimerWithTimeInterval:3 target:nil selector:nil userInfo:nil repeats:NO];
        NSLog(@"runing.it works!\n");


        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
      }

    }


void stopSession() { 
        [session stopRunning];
}

问题是因为您正在尝试启动一个已经在运行的会话。几乎不需要其他检查,就像会话是否已经启动、已经在运行等。抛出几个标志来检查这些标志并尝试!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21311357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档