iphone - 录制iphone时视频放大和缩小

标签 iphone ios avfoundation

我正在开发一个应用程序,用户可以在其中放大和缩小视频。我正在使用 AV Foundation 录制视频,但我无法在用户录制视频时放大和缩小视频。

我正在使用下面的代码来录制视频

    - (void) startCamera
    NSLog(@"Setting up capture session");
        CaptureSession = [[AVCaptureSession alloc] init];

        //----- ADD INPUTS -----
        NSLog(@"Adding video input");

        //ADD VIDEO INPUT
        AVCaptureDevice *VideoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if (VideoDevice)
        {
            NSError *error;
            VideoInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:VideoDevice error:&error];
            if (!error)
            {
                if ([CaptureSession canAddInput:VideoInputDevice])
                    [CaptureSession addInput:VideoInputDevice];
                else
                    NSLog(@"Couldn't add video input");
            }
            else
            {
                NSLog(@"Couldn't create video input");
            }
        }
        else
        {
            NSLog(@"Couldn't create video capture device");
        }

        //ADD AUDIO INPUT
        NSLog(@"Adding audio input");
        AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
        NSError *error = nil;
        AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
        if (audioInput)
        {
            [CaptureSession addInput:audioInput];
        }


        //----- ADD OUTPUTS -----

        //ADD VIDEO PREVIEW LAYER
        NSLog(@"Adding video preview layer");
        [self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:CaptureSession]];

        PreviewLayer.orientation = AVCaptureVideoOrientationPortrait;       //<<SET ORIENTATION.  You can deliberatly set this wrong to flip the image and may actually need to set it wrong to get the right image

        [[self PreviewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];


        //ADD MOVIE FILE OUTPUT
        NSLog(@"Adding movie file output");
        MovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

        Float64 TotalSeconds = 60;          //Total seconds
        int32_t preferredTimeScale = 30;    //Frames per second
        CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale);   //<<SET MAX DURATION
        MovieFileOutput.maxRecordedDuration = maxDuration;

        MovieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;                        //<<SET MIN FREE SPACE IN BYTES FOR RECORDING TO CONTINUE ON A VOLUME

        if ([CaptureSession canAddOutput:MovieFileOutput])
            [CaptureSession addOutput:MovieFileOutput];

        //SET THE CONNECTION PROPERTIES (output properties)
        [self CameraSetOutputProperties];           //(We call a method as it also has to be done after changing camera)



        //----- SET THE IMAGE QUALITY / RESOLUTION -----
        //Options:
        //  AVCaptureSessionPresetHigh - Highest recording quality (varies per device)
        //  AVCaptureSessionPresetMedium - Suitable for WiFi sharing (actual values may change)
        //  AVCaptureSessionPresetLow - Suitable for 3G sharing (actual values may change)
        //  AVCaptureSessionPreset640x480 - 640x480 VGA (check its supported before setting it)
        //  AVCaptureSessionPreset1280x720 - 1280x720 720p HD (check its supported before setting it)
        //  AVCaptureSessionPresetPhoto - Full photo resolution (not supported for video output)
        NSLog(@"Setting image quality");
        [CaptureSession setSessionPreset:AVCaptureSessionPresetMedium];
        if ([CaptureSession canSetSessionPreset:AVCaptureSessionPreset640x480])     //Check size based configs are supported before setting them
            [CaptureSession setSessionPreset:AVCaptureSessionPreset640x480];



        //----- DISPLAY THE PREVIEW LAYER -----
        //Display it full screen under out view controller existing controls
        NSLog(@"Display the preview layer");
        CGRect layerRect;
        layerRect = CGRectMake(284, 0, 200, 200);

        [PreviewLayer setBounds:layerRect];
        [PreviewLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),
                                              CGRectGetMidY(layerRect))];
        //[[[self view] layer] addSublayer:[[self CaptureManager] previewLayer]];
        //We use this instead so it goes on a layer behind our UI controls (avoids us having to manually bring each control to the front):
        UIView *CameraView = [[UIView alloc] init];
        [[self view] addSubview:CameraView];
        [self.view sendSubviewToBack:CameraView];

        [[CameraView layer] addSublayer:PreviewLayer];


        //----- START THE CAPTURE SESSION RUNNING -----
        [CaptureSession startRunning];
}

//********** CAMERA SET OUTPUT PROPERTIES **********
- (void) CameraSetOutputProperties
{
    //SET THE CONNECTION PROPERTIES (output properties)
    AVCaptureConnection *CaptureConnection = [MovieFileOutput connectionWithMediaType:AVMediaTypeVideo];

    //Set landscape (if required)
    if ([CaptureConnection isVideoOrientationSupported])
    {
        AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationPortrait;      //<<<<<SET VIDEO ORIENTATION IF LANDSCAPE
        [CaptureConnection setVideoOrientation:orientation];
    }

    //Set frame rate (if requried)
    CMTimeShow(CaptureConnection.videoMinFrameDuration);
    CMTimeShow(CaptureConnection.videoMaxFrameDuration);

    if (CaptureConnection.supportsVideoMinFrameDuration)
        CaptureConnection.videoMinFrameDuration = CMTimeMake(1, CAPTURE_FRAMES_PER_SECOND);
    if (CaptureConnection.supportsVideoMaxFrameDuration)
        CaptureConnection.videoMaxFrameDuration = CMTimeMake(1, CAPTURE_FRAMES_PER_SECOND);

    CMTimeShow(CaptureConnection.videoMinFrameDuration);
    CMTimeShow(CaptureConnection.videoMaxFrameDuration);
}

//********** DID FINISH RECORDING TO OUTPUT FILE AT URL **********
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
      fromConnections:(NSArray *)connections
                error:(NSError *)error
{

    NSLog(@"didFinishRecordingToOutputFileAtURL - enter");

    BOOL RecordedSuccessfully = YES;
    if ([error code] != noErr)
    {
        // A problem occurred: Find out if the recording was successful.
        id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
        if (value)
        {
            RecordedSuccessfully = [value boolValue];
        }
    }
    if (RecordedSuccessfully)
    {
        //----- RECORDED SUCESSFULLY -----
        NSLog(@"didFinishRecordingToOutputFileAtURL - success");
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputFileURL])
        {
            [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
                                        completionBlock:^(NSURL *assetURL, NSError *error)
             {
                 if (error)
                 {

                 }
             }];
        }

    }
}

任何帮助都会很棒

最佳答案

你不能缩放 Iphone 相机,最多你可以在拍照时使用 cameraViewTransform: 并对相机的实时 View 应用变换,但它仍然不会影响最终拍摄的照片

关于iphone - 录制iphone时视频放大和缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16516552/

相关文章:

iOS 7.1.1 OTA 分发 - 连接失败

ios - UITableview 重复复选标记

ios - avplayer无法播放通过IOS Objective-C中的设备相机捕获的视频中的音频

ios - AVFoundation 以 MP4 格式录制视频

iPhone:20 分钟后 MapView 中的应用程序崩溃

iPhone FBConnect 始终切换到 Safari 或 Facebook 应用程序进行身份验证

iphone - 强制 UITabView 到 'dock' 或锁定到屏幕底部?

ios - 为什么 subview Controller 会占据整个屏幕?

带有标签栏 Controller 的 iOS 数据

ios - 使用 Swift 进行视频缓冲输出