日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php開源 > 綜合技術(shù) > 利用AVFoundation實現(xiàn)Blink拍照和錄像的功能

利用AVFoundation實現(xiàn)Blink拍照和錄像的功能

來源:程序員人生   發(fā)布時間:2015-09-09 08:39:24 閱讀次數(shù):3947次

利用AVFoundation實現(xiàn)Blink拍照和錄相的功能

by 伍雪穎

github代碼

頭幾天偶然發(fā)現(xiàn)1個app叫Blink,閑來無事,純當練手,因而就嘗試下自己實現(xiàn)它的功能.
頁面都挺簡單的

1.打開相機

- (void)openCamera:(AVCaptureDevicePosition)cameraPostion {
   
BOOL hasCamera = ([[AVCaptureDevice devices] count] > 0);
   
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.獲得靜態(tài)圖片

- (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; }
    }
   
// 取靜態(tài)圖片
    [
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:@"%@%@", NSTemporaryDirectory(),@"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(0, 0, 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];
}

看了下它的網(wǎng)絡(luò)數(shù)據(jù)格式方面,Blink主要是打包成zip文件,里面包括json,mp4,png文件.
等有空了,把服務(wù)器也寫1下,或誰有興趣可以把它的服務(wù)器實現(xiàn)下.

固然Blink還有1些添加好友,分享等的功能,這些細節(jié)還是比較好實現(xiàn)的.

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 二区三区在线观看 | 亚洲专区久久 | 亚洲成人av一区二区 | 一区二区三区在线观看国产 | 午夜一级毛片 | 熟女毛毛多熟妇人妻aⅴ在线毛片 | 欧美一区二区日韩 | yw.139尤物在线精品视频 | 国产精品免费视频观看 | 免费福利影院 | 最新中文字幕免费视频 | 成人av日韩 | 色综合久 | 在线观看成人网 | 一区二区三区 在线 | 国产精品日韩在线观看一区二区 | 亚洲国产中文在线 | 午夜色播 | 欧州一区二区 | 黄色三级在线免费观看 | 99re最新视频 | 五月婷婷综合激情网 | 久久久久这里只有精品 | 日韩精品二区 | 欧美激情视频一区二区 | 国产精品久久久久一区二区三区 | 欧美,日韩,国产在线 | 日日夜夜天天综合 | 国产福利视频在线 | 日韩一区二区精品 | av二三区 | 免费在线a| 成人国产精品久久 | 婷婷久久综合九色综合绿巨人 | 欧美韩日一区 | 国产精品美女久久久久久久网站 | 国产一区二区三区免费在线观看 | 久久成人一区 | 亚洲一区二区三区免费观看 | 欧美久久一区 | 精品久久久久久久久久久久久久久 |