利用AVFoundation实现Blink拍照和录像的功能

利用AVFoundation实现Blink拍照和录像的功能,第1张

概述利用AVFoundation实现Blink拍照录像的功能

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

github代码 前几天偶然发现一个app叫Blink,闲来无事,纯当练手,于是就尝试下自己实现它的功能. 页面都挺简单的
1.打开相机 - (voID)openCamera:(AVCaptureDeviceposition)cameraPostion {
    BOol hasCamera = ([[AVCaptureDevice devices] count] > );
    if (hasCamera) {
        AVCaptureSession *session = [[AVCaptureSession alloc] init];
        session.sessionPreset = AVCaptureSessionPresetHigh;
       
        AVCaptureVIDeoPrevIEwLayer *captureVIDeoPrevIEwLayer = [[AVCaptureVIDeoPrevIEwLayer alloc] initWithSession:session];
        [captureVIDeoPrevIEwLayer setVIDeoGravity:AVLayerVIDeoGravityResizeAspectFill];
        [captureVIDeoPrevIEwLayer setFrame:self.cameraimageVIEw.bounds];
        [self.cameraimageVIEw.layer addSublayer:captureVIDeoPrevIEwLayer];
       
        AVCaptureDevice *device = [self getCamera:cameraPostion];
        NSError *error = nil;
       
        AVCaptureDeviceinput *input = [AVCaptureDeviceinput deviceinputWithDevice:device error:&error];
        [session addinput:input];
       
        stillimageOutput = [[AVCaptureStillimageOutput alloc] init];
        NSDictionary *outputSettings = @{ AVVIDeoCodecKey : AVVIDeoCodecJPEG};
        [stillimageOutput setoutputSettings:outputSettings];
        [session addOutput:stillimageOutput];
       
        movIEOutput = [[AVCaptureMovIEfileOutput alloc] init];
        [session addOutput:movIEOutput];
       
        [session startRunning];
    } }
- (AVCaptureDevice *)getCamera:(AVCaptureDeviceposition)cameraPostion {
    NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVIDeo];
    for (AVCaptureDevice *device in cameras) {
        if (device.position == cameraPostion)
            return device;
    }
    return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVIDeo]; }
2.获取静态图片 - (voID)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; }
    }
    // 取静态图片
    [stillimageOutput captureStillimageAsynchronouslyFromConnection:vIDeoConnection
              completionHandler: ^(CMSampleBufferRef imageSampleBuffer,NSError *error) {
                  NSData *imageData = [AVCaptureStillimageOutput jpegStillimageNSDataRepresentation:imageSampleBuffer];
                  NSData *compressedData = [imageData gzippedDataWithCompressionLevel:1.0];
                  NSData *outputData = [compressedData gunzippedData];
                  UIImage *imageT = [[UIImage alloc] initWithData:outputData];
                  _testimageVIEw.image = imageT;
                  _testimageVIEw.hIDden = NO;
                  NSfileManager *fileManager = [NSfileManager defaultManager];
                  Nsstring *filePath = [[Nsstring alloc] initWithFormat:@"%@%@",NstemporaryDirectory(),@"main.png"];
                  [fileManager createfileAtPath:filePath contents:imageData attributes:nil];
                 
                  Nsstring *zipfile = [[Nsstring alloc] initWithFormat:@"%@%@",@"main.zip"];
                  ZipArchive *za = [[ZipArchive alloc] init];
                  [za CreateZipfile2:zipfile];
                  [za addfileToZip:filePath newname:@"main.png"];
              }]; } 3.录像和播放录像 - (IBAction)beginRecord:(ID)sender {
    [movIEOutput startRecordingToOutputfileURL:[self fileURLWithname:@"main.mp4"] recordingDelegate:self]; }
- (voID)stopRecord {
    [movIEOutput stopRecording]; }
- (voID)captureOutput:(AVCapturefileOutput *)captureOutput
        dIDFinishRecordingToOutputfileAtURL:(NSURL *)outputfileURL
              fromConnections:(NSArray *)connections
                        error:(NSError *)error {
    BOol recordedSuccessfully = YES;
    if (error == nil) {
        [self playVIDeo:outputfileURL];
    }
    if ([error code] != noErr) {
        ID value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
        if (value) {
            recordedSuccessfully = [value boolValue];
           
        }
    } }
- (voID)playVIDeo:(NSURL *)url {
    AVPlayer *player = [AVPlayer playerWithURL:url];
    playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
   
    playerLayer.frame = CGRectMake(,160,284);
    playerLayer.position = self.vIEw.center;
    [self.vIEw.layer addSublayer:playerLayer];
   
    playerLayer.masksToBounds = YES;
    playerLayer.cornerRadius = 20;
    playerLayer.borderWIDth = 1;
    playerLayer.bordercolor = [UIcolor graycolor].CGcolor;
   
    [player play];
    [[NSNotificationCenter defaultCenter]
                         addobserver:self
                         selector:@selector(removePlayer)
                         name:AVPlayerItemDidplayToEndTimeNotification
                         object:nil]; }
- (voID)removePlayer {
    [playerLayer removeFromSuperlayer]; }
看了下它的网络数据格式方面,Blink主要是打包成zip文件,里面包含Json,mp4,png文件. 等有空了,把服务器也写一下,或者谁有兴趣可以把它的服务器实现下.
当然Blink还有一些添加好友,分享等的功能,这些细节还是比较好实现的.

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的利用AVFoundation实现Blink拍照和录像的功能全部内容,希望文章能够帮你解决利用AVFoundation实现Blink拍照和录像的功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1106318.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存