[英]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.