abhi abhi - 5 months ago 12
iOS Question

how to make top edge of the waveform rounded

I want to create

top edge
and
bottom edge
of wave forms in rounded shape .

currently i am able to get these
wave forms


enter image description here

the code below is

- (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;
}

Answer

Try this, 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;
    }