如何使波形的上边缘圆滑

时间:2016-06-29 10:17:10

标签: ios iphone xcode

我想创建圆形波形的top edgebottom edge

目前我可以获得这些wave forms

enter image description here

以下代码是

 - (UIImage*) drawImageFromSamples:(SInt16*)samples
                     maxValue:(SInt16)maxValue
                  sampleCount:(NSInteger)sampleCount {

CGSize imageSize = CGSizeMake(sampleCount * (_drawSpaces ? 6 : 6), self.height);
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);
CGContextSetAlpha(context, 1.0);

CGRect rect;
rect.size = imageSize;
rect.origin.x = 0;
rect.origin.y = 0;

CGColorRef waveColor = self.waveColor.CGColor;

CGContextFillRect(context, rect);

CGContextSetLineWidth(context, 2.0);

float channelCenterY = imageSize.height / 2;
float sampleAdjustmentFactor = imageSize.height / (float)maxValue;

for (NSInteger i = 0; i < sampleCount; i++)
{
    float val = *samples++;
    val = val * sampleAdjustmentFactor;
    if ((int)val == 0)
        val = 1.0; // draw dots instead emptyness
    CGContextMoveToPoint(context, i * (_drawSpaces ? 6 : 6), channelCenterY - val / 2.0);
    CGContextAddLineToPoint(context, i * (_drawSpaces ? 6 : 6), channelCenterY + val / 2.0);
    CGContextSetStrokeColorWithColor(context, waveColor);
    CGContextStrokePath(context);
}

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;
}

1 个答案:

答案 0 :(得分:0)

试试这个CGContextSetLineCap (context, kCGLineCapRound);

-(UIImage*) drawImageFromSamples:(SInt16*)samples maxValue:(SInt16)maxValue sampleCount:(NSInteger)sampleCount {

    CGSize imageSize = CGSizeMake(sampleCount * (_drawSpaces ? 6 : 6), self.height); UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor); CGContextSetAlpha(context, 1.0);

    CGRect rect; rect.size = imageSize; rect.origin.x = 0; rect.origin.y = 0;

    CGColorRef waveColor = self.waveColor.CGColor;

    CGContextFillRect(context, rect);

    CGContextSetLineWidth(context, 2.0);

// Start: Line Added to the code
        CGContextSetLineCap (context, kCGLineCapRound);
        // End: Line Added to the code
    float channelCenterY = imageSize.height / 2; float sampleAdjustmentFactor = imageSize.height / (float)maxValue;

    for (NSInteger i = 0; i < sampleCount; i++) { float val = *samples++; val = val * sampleAdjustmentFactor; if ((int)val == 0) val = 1.0; // draw dots instead emptyness

        CGContextMoveToPoint(context, i * (_drawSpaces ? 6 : 6), channelCenterY - val / 2.0);
        CGContextAddLineToPoint(context, i * (_drawSpaces ? 6 : 6), channelCenterY + val / 2.0);CGContextSetStrokeColorWithColor(context, waveColor); CGContextStrokePath(context); }

        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return newImage;
    }