[英]EZAudio iOS - Save Waveform to Image
我正在使用iOS的EZAudio库来处理音频文件的播放并绘制其波形。 我想用整个波形创建一个视图(EZAudioPlotGL视图,这是UIView的子类),然后将其另存为png。
我对此有几个问题:
这些图像中可以看到这些问题:
屏幕的外观如何(原始音频图):
屏幕的外观(显示我不想绘制到屏幕上的tempPlot):
我得到的保存图像应该是tempPlot的副本:
可以在这里找到EZAudio库: https : //github.com/syedhali/EZAudio
如果您想亲自了解问题,可以在这里找到我的项目: https : //www.dropbox.com/sh/8ilfaofvaa8aq3p/AADU5rOwqzCtEmJz-ePRXIDZa
我对OpenGL图形的经验不是很丰富,因此EZAudioPlotGL类中正在进行的许多工作都有些麻烦。
以下是相关代码:
ViewController.m:
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Customizing the audio plot's look
self.audioPlot.backgroundColor = [UIColor blueColor];
self.audioPlot.color = [UIColor whiteColor];
self.audioPlot.plotType = EZPlotTypeBuffer;
self.audioPlot.shouldFill = YES;
self.audioPlot.shouldMirror = YES;
// Try opening the sample file
[self openFileWithFilePathURL:[NSURL fileURLWithPath:kAudioFileDefault]];
}
-(void)openFileWithFilePathURL:(NSURL*)filePathURL {
self.audioFile = [EZAudioFile audioFileWithURL:filePathURL];
// Plot the whole waveform
[self.audioFile getWaveformDataWithCompletionBlock:^(float *waveformData, UInt32 length) {
[self.audioPlot updateBuffer:waveformData withBufferSize:length];
}];
//save whole waveform as image
[self.audioPlot fullWaveformImageForSender:self];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"waveformImage.png"];
[UIImagePNGRepresentation(self.snapshotImage) writeToFile:filePath atomically:YES];
}
@end
我的EZAudioPlotGL类别:
- (void)fullWaveformImageForSender:(ViewController *)sender{
EZAudioPlotGL *tempPlot = [[EZAudioPlotGL alloc]initWithFrame:self.frame];
[tempPlot setPlotType: EZPlotTypeBuffer];
[tempPlot setShouldFill: YES];
[tempPlot setShouldMirror: YES];
[tempPlot setBackgroundColor: [UIColor redColor]];
[tempPlot setColor: [UIColor greenColor]];
//plot full waveform on tempPlot
[sender.audioFile getWaveformDataWithCompletionBlock:^(float *waveformData, UInt32 length) {
[tempPlot updateBuffer:waveformData withBufferSize:length];
//tempPlot.glkVC is a getter for the private EZAudioPlotGLKViewController property in tempPlot (added by me in the EZAudioPlotGL class)
sender.snapshotImage = [((GLKView *)tempPlot.glkVC.view) snapshot];
}];
}
drawViewHierarchyInRect
仅适用于捕获基于CoreGraphics的视图绘图。 (CG绘制发生在CPU上,并渲染到主内存中的缓冲区中,因此CG,也称为UIGraphics
,只能从那里提取图像。)如果您的视图使用OpenGL绘制其内容,它将无济于事。 (OpenGL绘制在GPU上进行,因此您需要使用GL将像素从GPU读取回主内存,然后才能从它们中构建图像。)
看起来您的库使用GLKView
实例进行GLKView
。 (在源代码中EZAudioPlotGL
, EZAudioPlotGL
使用EZAudioPlotGLKViewController
,它创建了自己的GLKView
。)该类又具有一个snapshot
方法,该方法可以完成所有繁重的工作,以从GPU取回像素并将其放入UIImage
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.