我正在使用用于EZAudio的iOS库来处理音频文件的回放并绘制其波形。我想创建一个包含整个波形的视图( 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];
}
@endMy 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];
}];
}发布于 2014-04-28 17:12:41
drawViewHierarchyInRect只适用于捕获基于CoreGraphics的视图绘图。(CG绘图发生在CPU上,并呈现到主内存中的缓冲区中,因此CG,又名UIGraphics,只需将图像从其中输出。)如果您的视图使用OpenGL绘制其内容,它将不会对您有所帮助。(OpenGL绘图发生在GPU上,因此您需要使用GL将像素从GPU读取回主内存,然后才能用它们生成图像。)
看起来,您的库使用GLKView实例进行绘图。( EZAudioPlotGL在源代码中穿插,使用EZAudioPlotGLKViewController,它创建了自己的GLKView。)这个类又有一个snapshot方法,它完成所有繁重的工作,从GPU中获取像素并将它们放入UIImage中。
https://stackoverflow.com/questions/23330434
复制相似问题