Swift下 GPUImage 的安装与使用

Swift下 GPUImage 的安装与使用

1. 安装

先说说官方的建议:

Xcode 6 and iOS 8 support the use of full frameworks, as does the Mac, which simplifies the process of adding this to your application. To add this to your application, I recommend dragging the .xcodeproj project file into your application's project (as you would in the static library target).

For your application, go to its target build settings and choose the Build Phases tab. Under the Target Dependencies grouping, add GPUImageFramework on iOS (not GPUImage, which builds the static library) or GPUImage on the Mac. Under the Link Binary With Libraries section, add GPUImage.framework.

This should cause GPUImage to build as a framework. Under Xcode 6, this will also build as a module, which will allow you to use this in Swift projects. When set up as above, you should just need to use import GPUImage to pull it in.

You then need to add a new Copy Files build phase, set the Destination to Frameworks, and add the GPUImage.framework build product to that. This will allow the framework to be bundled with your application (otherwise, you'll see cryptic "dyld: Library not loaded: @rpath/GPUImage.framework/GPUImage" errors on execution).

事实上官方也不推荐使用 coacopods 安装, 那么大致的步骤就很明了了:

  1. 下载 GPUImage 项目源码

    git clone https://github.com/BradLarson/GPUImage.git
    
  2. 在 Finder 中, 直接把 GPUImage.xcodeproj 文件拖入到工程项目中

  3. 在 workspace 中编译 GPUImageFramework, 可以在 GPUImage 的 product group 下得到 GPUImage.framework 用于接下来的操作

  4. 在工程项目target设置中, 选择Build Phases, 在Link Binary With Libraries中添加GPUImage.framework.

  5. 同样在Build Phases里,点击左上角的加号, 添加一个新的 Cpoy file phase, Destination 选 Frameworks, 然后把之前编译的 GPUImage.framework 加入进来.

理论上这样就可以在 Siwft 代码中直接用import GPUImage导入 GPUImage 类了.

2. demo运行

参考SimpleSwiftVideoFilterExample:

@IBOutlet weak var previewView: GPUImageView!
    
var videoCamera: GPUImageVideoCamera?
var filter: GPUImageFilter?
    
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    self.videoCamera = GPUImageVideoCamera(sessionPreset: AVCaptureSessionPreset640x480, cameraPosition: .Front)
    self.videoCamera!.outputImageOrientation = .Portrait;
    self.filter = GPUImagePixellateFilter()
    self.videoCamera?.addTarget(filter)
    self.filter?.addTarget(self.previewView as GPUImageView)
    self.videoCamera?.startCameraCapture()
}

3. 可能遇到的坑

  1. 手贱 build 了documentation, 可能会发现缺少appledoc, 直接用 brew 安装一个即可:

    brew install appledoc
    
  2. demo代码中开始想把 videoCamerafilter 的申明放在 viewDidLoad 方法内, 但后来发现这样做无法正常显示视频, 可能是 GPUImage 相关对象被释放了.

你可能感兴趣的:(Swift下 GPUImage 的安装与使用)