私はアプリで画像処理を行っています。ライブビデオを撮影し、その上に画像を追加してオーバーレイとして使用します。残念ながら、これは、プログラムの他の部分が遅くなり、意図したとおりに動作しないようにするために大量のCPUを使用しています。基本的に、私は次のコードをCPUの代わりにGPUを使用したいと思っています。順番にiOSでGPUを使用して別の画像(ビデオフレーム)に1つの画像を重ねる
- (UIImage *)processUsingCoreImage:(CVPixelBufferRef)input {
CIImage *inputCIImage = [CIImage imageWithCVPixelBuffer:input];
// Use Core Graphics for this
UIImage * ghostImage = [self createPaddedGhostImageWithSize:CGSizeMake(1280, 720)];//[UIImage imageNamed:@"myImage"];
CIImage * ghostCIImage = [[CIImage alloc] initWithImage:ghostImage];
CIFilter * blendFilter = [CIFilter filterWithName:@"CISourceAtopCompositing"];
[blendFilter setValue:ghostCIImage forKeyPath:@"inputImage"];
[blendFilter setValue:inputCIImage forKeyPath:@"inputBackgroundImage"];
CIImage * blendOutput = [blendFilter outputImage];
EAGLContext *myEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
NSDictionary *contextOptions = @{ kCIContextWorkingColorSpace : [NSNull null] ,[NSNumber numberWithBool:NO]:kCIContextUseSoftwareRenderer};
CIContext *context = [CIContext contextWithEAGLContext:myEAGLContext options:contextOptions];
CGImageRef outputCGImage = [context createCGImage:blendOutput fromRect:[blendOutput extent]];
UIImage * outputImage = [UIImage imageWithCGImage:outputCGImage];
CGImageRelease(outputCGImage);
return outputImage;}
あなたはこのコードをプロファイリングしましたか? 'UIGraphicsBeginImageContext' /' UIGraphicsEndImageContext'の組み合わせは、その背後にあるメモリ配分のためにかなりのコストがかかります。すべてのコンポジションで新しいコンテキストを作成して破壊するだけでは、かなりうまくいくでしょう。 – Tommy
問題はメモリではありません。実際にはかなり低いですが、イメージをビデオフレームにオーバーレイしてRTMP経由で送信すると、CPUが20%から95%にジャンプします。私はそれがビデオフレームが圧縮されているレートで混乱して95%のマークを取得するように、CPUを低く保つようにGPUで取得することを望んでいました。 – Charlie
私はそれが相当の処理コストであると賭けています。メモリ割り当ては、同期とOS内部の非O(1)構造調整を意味します。 GPUへの切り替えが問題を解決するための非常に鈍い方法かもしれないことを示唆しているだけです。 '[UIImage imageNamed:]'がコストをかけているかを調べるためにプロファイルする必要があります - 'imageNamed:'はキャッシュの対象ですが、それは間違いなく速くなることを意味します。 – Tommy