GPUImage之拍照篇

fpkoko發表於2017-12-13

上篇講到初始化.這篇是拍照. 首先定義變數

//可以理解為裝置
GPUImageStillCamera* imageCamera;
//filter濾鏡
GPUImageFilter* filter;
//顯示出來的view
GPUImageView* iv;
複製程式碼

這裡要講一下GPUImageStillCamera GPUImageStillCamera繼承關係如下: GPUImageStillCamera->GPUImageVideoCamera->GPUImageOutput可以看到GPUImageStillCamera繼承自GPUImageVideoCamera,所以他就擁有了錄影的功能.既可以拍照.又可以錄影.

三個變數的連結關係如下: GPUImageStillCamera->GPUImageFilter->GPUImageView 這樣,攝像機轉到濾鏡再轉到view上顯示出來.

定義如下:

    imageCamera=[[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1920x1080 cameraPosition:AVCaptureDevicePositionBack];
    //AVCaptureDevicePositionBack為後攝像頭 front為前置攝像頭
    //AVCaptureSessionPreset1920x1080為解析度 另外還支援多種解析度
    //AVCaptureSessionPreset1280x720 等等等等
   
   imageCamera.outputImageOrientation=UIInterfaceOrientationPortrait;
   
    filter=[[GPUImageFilter alloc] init]; //預設濾鏡.
    /*官方版本如下:
    GPUImageFilter *customFilter = [[GPUImageFilter alloc] initWithFragmentShaderFromFile:@"CustomShader"];
    這個CustomShader是自定義的濾鏡檔案.如果沒有,那就使用預設濾鏡
    */
    [imageCamera addTarget:filter];
    
    iv=[[GPUImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.width)];
    iv.fillMode=kGPUImageFillModePreserveAspectRatioAndFill;
    /*顯示模式分為三種
    typedef NS_ENUM(NSUInteger, GPUImageFillModeType) {
    kGPUImageFillModeStretch,                       // Stretch to fill the full view, which may distort the image outside of its normal aspect ratio
    kGPUImageFillModePreserveAspectRatio,           // Maintains the aspect ratio of the source image, adding bars of the specified background color
    kGPUImageFillModePreserveAspectRatioAndFill     // Maintains the aspect ratio of the source image, zooming in on its center to fill the view
};
*/
    [filter addTarget:iv];
    [self.view addSubview:iv];//將顯示view加入父類
    [imageCamera startCameraCapture];//開啟攝像頭
複製程式碼

拍照程式碼:

    [imageCamera capturePhotoAsImageProcessedUpToFilter:testFilter withCompletionHandler:^(UIImage *processedImage, NSError *error) {
    if(error){
  return;
    }
        //存入本地相簿
        UIImageWriteToSavedPhotosAlbum(processedImage, nil, nil, nil);
    }];

複製程式碼

在結束的時候停止

    [imageCamera stopCameraCapture];
複製程式碼

下一篇寫錄影.後面還有各濾鏡的使用什麼的.然後再放出demo.

相關文章