当前位置:编程学习 > wap >>

iOS 画平滑曲线的方法及取音频数据的方法

ViewController.h
[plain] 
//  
//  ViewController.h  
//  iOS心电图Demo  
//  
//  Created by 杜甲 on 13-10-18.  
//  Copyright (c) 2013年 杜甲. All rights reserved.  
//  
  
#import <UIKit/UIKit.h>  
#import "HLSonogramView.h"  
@interface ViewController : UIViewController  
@property (strong, nonatomic) HLSonogramView* hlSonogramView;  
  
@end  
 
ViewController.m
[objc] 
//  
//  ViewController.m  
//  iOS心电图Demo  
//  
//  Created by 杜甲 on 13-10-18.  
//  Copyright (c) 2013年 杜甲. All rights reserved.  
//  
  
#import "ViewController.h"  
  
@interface ViewController ()  
  
@end  
  
@implementation ViewController  
  
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    // Do any additional setup after loading the view, typically from a nib.  
      
    self.hlSonogramView = [[HLSonogramView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];  
      
      
    [self.view addSubview:self.hlSonogramView];  
}  
  
- (void)didReceiveMemoryWarning  
{  
    [super didReceiveMemoryWarning];  
    // Dispose of any resources that can be recreated.  
}  
  
@end  
 
HLSonogramView.h
[objc] 
//  
//  HLSonogramView.h  
//  iOS心电图Demo  
//  
//  Created by 杜甲 on 13-10-18.  
//  Copyright (c) 2013年 杜甲. All rights reserved.  
//  
  
#import <UIKit/UIKit.h>  
#define kMaxKeyPoints  1000  
#define kHillSegmentWidth 10  
  
struct ret_value  
{  
    unsigned charchar *data;//注意这里是unsigned char  
    unsigned long int size;  
};  
  
@interface HLSonogramView : UIView  
{  
     CGPoint m_pSonogramKeyPoint[kMaxKeyPoints];  
}  
@property (assign ,nonatomic) float m_pOffsetX;  
@property (assign ,nonatomic) int m_pSonogramKeyPointNum;  
//转换后的座标数据,用于绘制波形图  
@property (nonatomic, strong) NSMutableArray *m_pointWavArray;  
@end  
 
HLSonogramView.m
[objc] 
//  
//  HLSonogramView.m  
//  iOS心电图Demo  
//  
//  Created by 杜甲 on 13-10-18.  
//  Copyright (c) 2013年 杜甲. All rights reserved.  
//  
  
#import "HLSonogramView.h"  
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height  
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width  
@implementation HLSonogramView  
  
- (id)initWithFrame:(CGRect)frame  
{  
    self = [super initWithFrame:frame];  
    if (self) {  
        // Initialization code  
        self.backgroundColor = [UIColor whiteColor];  
        [self generatePoint];  
    }  
    return self;  
}  
  
-(void)drawRect:(CGRect)rect  
{  
    [super drawRect:rect];  
    CGContextRef context = UIGraphicsGetCurrentContext();  
    CGContextSetLineCap(context, kCGLineCapSquare);  
    CGContextSetLineWidth(context, 1.0);  
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);  
    CGContextBeginPath(context);  
    CGContextMoveToPoint(context, 0, ScreenHeight / 2);  
    for (int i = 1; i < kMaxKeyPoints; i++) {  
          
        CGPoint p0 = m_pSonogramKeyPoint[i - 1];  
        CGPoint p1 = m_pSonogramKeyPoint[i];  
          
        int hSegments = floorf((p1.x - p0.x) / kHillSegmentWidth);  
        float dx = (p1.x - p0.x) / hSegments;  
        float da = M_PI / hSegments;  
        float ymid = (p0.y + p1.y) / 2;  
        float ampl = (p0.y - p1.y) / 2;  
          
          
        CGPoint pt0,pt1;  
        pt0 = p0;  
        for (int j = 0; j < hSegments + 1; ++j) {  
            pt1.x = p0.x + j * dx;  
            pt1.y = ymid + ampl * cosf(da * j);  
            CGContextAddLineToPoint(context, pt0.x, pt0.y);  
            CGContextAddLineToPoint(context, pt1.x, pt1.y);  
            pt0 = pt1;  
        }     
    }  
    CGContextStrokePath(context);  
}  
  
-(void)generatePoint  
{  
    float m_pWinHeight = ScreenHeight;  
    float m_pWinWidth  = ScreenWidth;  
      
      
    float x  = 0;  
    float y = m_pWinHeight / 2;  
         
    for (int i = 0; i < kMaxKeyPoints; ++i) {  
        m_pSonogramKeyPoint[i] = CGPointMake(x, y);  
        x += m_pWinWidth / 2;  
        y = rand() % (int)m_pWinHeight;  
    }  
}  
//取音频数据  
- (void)transformDateOFWavFromFile  
{  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"];  
    struct ret_valu
补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,