你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS GPUImage研究二:捕獲圖像stillCamera寫入相冊

iOS GPUImage研究二:捕獲圖像stillCamera寫入相冊

編輯:IOS開發綜合

本片介紹關於捕獲圖像stillCamera的研究:xoxo_x 著

IOS GPUImage研究二:捕獲圖像stillCamera寫入相冊

本文參考:
https://github.com/BradLarson/GPUImage#gpuimage

demo下載地址:
https://github.com/BradLarson/GPUImage/tree/master/examples/IOS/SimplePhotoFilter

步驟 內容 第一步 創建預覽View 即必須的GPUImageView 第二步 創建濾鏡 即這裡我們使用的 GPUImageSketchFilter(黑白反色) 第三步 創建Camera 即我們要用到的GPUImageStillCamera 第四步 addTarget 並開始處理startCameraCapture 第五步 回調數據、寫入相冊

IOS GPUImage研究二:捕獲圖像stillCamera寫入相冊

第一步:創建預覽View 即必須的GPUImageView

GPUImageView *primaryView = [[GPUImageView alloc] initWithFrame:mainScreenFrame];
    primaryView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

第二步:創建濾鏡 即這裡我們使用的 GPUImageSketchFilter(黑白反色)

   GPUImageSketchFilter *filter = [[GPUImageSketchFilter alloc] init];

第三步:創建Camera 即我們要用到的GPUImageStillCamera

GPUImageStillCamera* stillCamera = [[GPUImageStillCamera alloc] init];

//設置相機方向
stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

第四步: addTarget 並開始處理startCameraCapture

    [stillCamera addTarget:filter];
    [filter addTarget:primaryView];
    [stillCamera startCameraCapture];

第五步:添加一個按鈕photoCaptureButton,當按鈕點擊的時候進行以下處理,保存圖片到相冊

[photoCaptureButton setEnabled:NO];

    [stillCamera capturePhotoAsJPEGProcessedUpToFilter:filter withCompletionHandler:^(NSData *processedJPEG, NSError *error){

        // Save to assets library
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

        [library writeImageDataToSavedPhotosAlbum:processedJPEG metadata:stillCamera.currentCaptureMetadata completionBlock:^(NSURL *assetURL, NSError *error2)
         {
             if (error2) {
                 NSLog(@"ERROR: the image failed to be written");
             }
             else {
                 NSLog(@"PHOTO SAVED - assetURL: %@", assetURL);
             }

             runOnMainQueueWithoutDeadlocking(^{
                 [photoCaptureButton setEnabled:YES];
             });
         }];
    }];
處理分析:

一、分析:GPUImageStillCamera

@interface GPUImageStillCamera : GPUImageVideoCamera

繼承於GPUImageVideoCamera

@interface GPUImageVideoCamera : GPUImageOutput <AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAudioDataOutputSampleBufferDelegate>

GPUImageVideoCamera繼承於GPUImageOutput,並遵守AVCaptureVideoDataOutputSampleBufferDelegate、AVCaptureAudioDataOutputSampleBufferDelegate這兩個協議。分別是圖像數據和音頻數據的回調。

Filter:(GPUImageOutput *)finalFilterInChain
其中傳入的參數為 GPUImageOutput的子類

//這是寫入相冊方法,能用jpeg、或者png 或者 不作處理


-  (void)capturePhotoAsSampleBufferWithCompletionHandler:(void (^)(CMSampleBufferRef imageSampleBuffer, NSError *error))block;
- (void)capturePhotoAsImageProcessedUpToFilter:(GPUImageOutput<GPUImageInput> *)finalFilterInChain withCompletionHandler:(void (^)(UIImage *processedImage, NSError *error))block;
- (void)capturePhotoAsImageProcessedUpToFilter:(GPUImageOutput<GPUImageInput> *)finalFilterInChain withOrientation:(UIImageOrientation)orientation withCompletionHandler:(void (^)(UIImage *processedImage, NSError *error))block;
- (void)capturePhotoAsJPEGProcessedUpToFilter:(GPUImageOutput<GPUImageInput> *)finalFilterInChain withCompletionHandler:(void (^)(NSData *processedJPEG, NSError *error))block;
- (void)capturePhotoAsJPEGProcessedUpToFilter:(GPUImageOutput<GPUImageInput> *)finalFilterInChain withOrientation:(UIImageOrientation)orientation withCompletionHandler:(void (^)(NSData *processedJPEG, NSError *error))block;
- (void)capturePhotoaspNGProcessedUpToFilter:(GPUImageOutput<GPUImageInput> *)finalFilterInChain withCompletionHandler:(void (^)(NSData *processedPNG, NSError *error))block;
- (void)capturePhotoaspNGProcessedUpToFilter:(GPUImageOutput<GPUImageInput> *)finalFilterInChain withOrientation:(UIImageOrientation)orientation withCompletionHandler:(void (^)(NSData *processedPNG, NSError *error))block;

二、分析:ALAssetsLibrary

這個是Album(相冊)管理的類、是多媒體庫、用戶能夠查看到這些文件並能進行操作的;

文檔中這樣寫:

能夠創建相冊簿、能夠將CGImageRef、NSData、videoPathURL等寫入相冊。


- (void)addAssetsGroupAlbumWithName:(NSString *)name resultBlock:(ALAssetsLibraryGroupResultBlock)resultBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock NS_DEPRECATED_IOS(5_0, 9_0, "Use creationRequestForAssetCollectionWithTitle: on PHAssetCollectionChangeRequest from the Photos framework to create a new asset collection instead");

// These methods can be used to add photos or videos to the saved photos album.

// With a UIImage, the API user can use -[UIImage CGImage] to get a CGImageRef, and cast -[UIImage imageOrientation] to ALAssetOrientation.
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_0, 9_0, "Use creationRequestForAssetFromImage: on PHAssetChangeRequest from the Photos framework to create a new asset instead");

// The API user will have to specify the orientation key in the metadata dictionary to preserve the orientation of the image
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_1, 9_0, "Use creationRequestForAssetFromImage: on PHAssetChangeRequest from the Photos framework to create a new asset instead");

// If there is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata values will be overwritten
- (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_1, 9_0, "Use creationRequestForAssetFromImageData: on PHAssetChangeRequest from the Photos framework to create a new asset instead");

- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_0, 9_0, "Use creationRequestForAssetFromVideoAtFilePath: on PHAssetChangeRequest from the Photos framework to create a new asset instead");
- (BOOL)videoAtPathIsCompatibleWithSavedPhotosAlbum:(NSURL *)videoPathURL NS_DEPRECATED_IOS(5_0, 9_0);

三、分析GPUImageSketchFilter

詳細的濾鏡分析介紹:
http://blog.csdn.net/xoxo_x/article/details/57082804

這裡簡單說明:

將視像轉換為外觀像草圖。這只是Sobel邊緣檢測濾鏡的顏色反轉

@interface GPUImageSketchFilter : GPUImageSobelEdgeDetectionFilter

GPUImageSobelEdgeDetectionFilter繼承GPUImageSobelEdgeDetectionFilter

GPUImageSobelEdgeDetectionFilter:Sobel邊緣檢測,邊緣以白色突出顯示

texelWidth:
texelHeight:這些參數影響檢測到的邊緣的可見性
edgeStrength:調整濾波器的動態范圍。值越高,邊緣越強,但可以使強度色空間飽和。
本文涉及到的代碼:
#import <UIKit/UIKit.h>
#import "GPUImage.h"

@interface PhotoViewController : UIViewController
{
    GPUImageStillCamera *stillCamera;
    GPUImageOutput<GPUImageInput> *filter, *secondFilter, *terminalFilter;

}
- (void)viewDidLoad
{
[super viewDidLoad];

GPUImageView *primaryView = [[GPUImageView alloc] initWithFrame:mainScreenFrame];
primaryView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

stillCamera = [[GPUImageStillCamera alloc] init];
aPosition:AVCaptureDevicePositionBack];
stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

filter = [[GPUImageSketchFilter alloc] 


[stillCamera addTarget:filter];

GPUImageView *filterView = (GPUImageView *)self.view;


[stillCamera startCameraCapture];


  [photoCaptureButton setEnabled:NO];

    [stillCamera capturePhotoAsJPEGProcessedUpToFilter:filter withCompletionHandler:^(NSData *processedJPEG, NSError *error){

        // Save to assets library
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

        [library writeImageDataToSavedPhotosAlbum:processedJPEG metadata:stillCamera.currentCaptureMetadata completionBlock:^(NSURL *assetURL, NSError *error2)
         {
             if (error2) {
                 NSLog(@"ERROR: the image failed to be written");
             }
             else {
                 NSLog(@"PHOTO SAVED - assetURL: %@", assetURL);
             }

             runOnMainQueueWithoutDeadlocking(^{
                 [photoCaptureButton setEnabled:YES];
             });
         }];
    }];

以上就是iOS GPUImage研究二:捕獲圖像stillCamera寫入相冊的全文介紹,希望對您學習和使用ios應用開發有所幫助.

【iOS GPUImage研究二:捕獲圖像stillCamera寫入相冊】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved