Striped Lines appear out of nowhere - ios

I am trying to code a routine that would allow me to create a UIImage that is a rectangle filled with stripes on a gradient background.
My code is below and works fine for most of the cases I tried it out. Interestingly, and this is the hook, it doesn't work when I pass it 21 as height and 5 as stripedWidth.
Once I do this, the stripes appear as they should... horizontally.. but vertically they start at like (y=) -40 and end at about (y=) 4 or so. To see this better, each this image showing the effect in question:
Has anyone any idea why this is happening, or even better, what I can do against it?
-(UIImage*) stripedTextureWithStartingColor:(UIColor*) startColor withEndingColor:(UIColor*) endColor withHeight:(NSUInteger) height withStripeWidth:(NSUInteger) stripeWidth withStripeColor:(UIColor*) stripedColor {
CGSize size = CGSizeMake(2 * stripeWidth, height);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
#try {
CGContextSetAllowsAntialiasing(context, true);
CGContextSetShouldAntialias(context, true);
NSArray* colors = [NSArray arrayWithObjects:(id) startColor.CGColor, (id) endColor.CGColor, nil];
CGFloat locations[2];
locations[0] = 0.0;
locations[1] = 1.0;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
CGColorSpaceRelease(colorSpace);
CGContextDrawLinearGradient(context, gradient, CGPointZero, CGPointMake(0, size.height - 1), 0);
CGGradientRelease(gradient);
CGContextFillPath(context);
int lineWidth = (int) stripeWidth;
CGContextSetLineWidth(context, lineWidth / 2);
int lineCount = (float) size.height / (float) lineWidth;
lineCount -= 2;
for (int i=0; i<lineCount; i++) {
CGContextSetStrokeColorWithColor(context, stripedColor.CGColor);
float x1 = -size.height + i * 2 * lineWidth - lineWidth;
float y1 = size.height - 1 + lineWidth;
float x2 = -size.height + i * 2 * lineWidth + size.height - lineWidth;
float y2 = -lineWidth;
CGContextMoveToPoint(context, x1, y1);
CGContextAddLineToPoint(context, x2, y2);
CGContextStrokePath(context);
}
UIColor* lineTopColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9];
UIColor* lineBottomColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.5];
CGContextSetStrokeColorWithColor(context, lineTopColor.CGColor);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, size.width + 1, 0);
CGContextStrokePath(context);
CGContextSetStrokeColorWithColor(context, lineBottomColor.CGColor);
CGContextMoveToPoint(context, 0, size.height - 1);
CGContextAddLineToPoint(context, size.width + 1, size.height - 1);
CGContextStrokePath(context);
}
#finally {
CGContextRestoreGState(context);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

Found it, my algorithm was slightly incorrect with the starting of the lines

Related

How fill MKCircleRenderer with blur color

I have implemented custom implementation of MKCircleRenderer.I need to have a circle with attached color. I can fill it with specific color. But how can add blur effect?
Here is my code:
- (void)fillPath:(CGPathRef)path inContext:(CGContextRef)context_{
if(!distance)
return;
#synchronized (self) {
if(!image){
rect = CGPathGetBoundingBox(path);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context, path);
CGContextSetBlendMode (context, kCGBlendModePlusDarker);
CGContextClip(context);
CGFloat gradientLocations[2] = {0, 1.0f};
UIColor* color = [UIColor colorWithHTMLString:#"#27B255"];
const CGFloat* components = CGColorGetComponents(color.CGColor);
CGFloat gradientColors[8] = {components[0], components[1], components[2], 0.3, components[0], components[1], components[2], 0.3};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradientColors, gradientLocations, 2);
CGColorSpaceRelease(colorSpace);
CGPoint gradientCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
CGFloat gradientRadius = MIN(rect.size.width, rect.size.height) / 2;
CGContextDrawRadialGradient(context, gradient, gradientCenter, 0, gradientCenter, gradientRadius, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
if(distance.accuracy > 0){
CGFloat k = (CGFloat)(rect.size.width/(2*(distance.distance + distance.accuracy)));
CGFloat radius = (distance.distance - distance.accuracy) * k;
if(radius > 0){
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextAddArc(context, CGRectGetMidX(rect), CGRectGetMidY(rect), radius, 0, 2 * M_PI, 0);
CGContextDrawPath(context, kCGPathFill);
CGContextSetBlendMode(context, kCGBlendModeNormal);
}
}
image = UIGraphicsGetImageFromCurrentImageContext();
image = [UIImageEffects imageByApplyingBlurToImage:image withRadius:1 tintColor:nil saturationDeltaFactor:1.0 maskImage:nil];
UIGraphicsEndImageContext();
}
}
CGContextDrawImage(context_, rect, image.CGImage);
}
I use UIImageEffects classes from Apple, but no result that I expect. Could you provide any other varian how to do this?

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

Why is Core Graphics drawing this outline?

For some reason Core Graphics is drawing an extra outline around my path. Here is some sample code.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSaveGState(context);
float startRadius = 71.0;
float startAngle = 135, endAngle = 405;
endAngle = startAngle + (endAngle - startAngle)*drawValue;
int clockwise = 0;
if(isTargetFiller) {
if(drawValue < 0.0)
clockwise = 1;
startAngle = 270;
endAngle = 405;
endAngle = startAngle + (endAngle - startAngle)*drawValue;
}
CGPoint center = CGPointMake(75, 75);
CGContextMoveToPoint(context, center.x, center.y);
CGContextAddArc(context, center.x, center.y, startRadius, DEG_2_RAD(startAngle),
DEG_2_RAD(endAngle), clockwise);
CGContextClosePath(context);
CGContextClip(context);
UIImage *bigImage = [UIImage imageNamed:#"CTracker_Full.png"];
CGPoint topLeftOffset = CGPointMake(
(self.bounds.size.width - bigImage.size.width) / 2,
(self.bounds.size.height - bigImage.size.height) / 2);
[bigImage drawAtPoint: topLeftOffset blendMode:kCGBlendModeNormal alpha:1.0];
CGContextSetBlendMode (context, kCGBlendModeMultiply);
float green = 1.0;
float red = 0.0;
if(value > 0.5) {
green = 1.0 - (value - 0.5) / 0.5;
red = (value - 0.5) / 0.5;
}
UIColor *tintColor = [UIColor colorWithRed:red green:green blue:0.0 alpha:1.0];
CGContextSetFillColor(context, CGColorGetComponents(tintColor.CGColor));
CGContextFillRect(context, rect);
CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
[bigImage drawAtPoint:topLeftOffset blendMode:kCGBlendModeDestinationIn
alpha:1.0];
Here is what it is currently drawing:
As you can see, there is an outline around the actual shape that leads to the center of the image.
Figured it out myself right after posting this. Hopefully this will help someone else.
Changed the last little bit of code to:
UIColor *tintColor = [UIColor colorWithRed:red green:green blue:0.0 alpha:1.0];
CGContextSetFillColor(context, CGColorGetComponents(tintColor.CGColor));
CGContextFillRect(context, rect);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
[bigImage drawAtPoint:topLeftOffset blendMode:kCGBlendModeDestinationIn alpha:1.0];

Fast way to redraw gradient?

I need to draw (a bit) complex gradients programmatically. Here is my params.
#define SHADOW_OPACITY 0.35
const size_t num_locations = 4;
const CGFloat locations[num_locations] = { 0, 0.12, 0.42, 1.0 };
const CGFloat components[num_locations * 2] = { 0.0, 1.0 * SHADOW_OPACITY,
0.0, 1.0 * SHADOW_OPACITY,
0.0, 0.73 * SHADOW_OPACITY,
0.0, 0.0 };
Three rings with opacity and a dashed ring inside. All works just fine.
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawGradientInContext:context];
CGContextSetLineWidth(context, CIRCLE_STROKE_WIDTH);
CGContextBeginPath(context);
CGFloat dashLength = DASH_LENGTH(self.circleRadius);
CGFloat dashArray[] = { dashLength, dashLength };
CGContextSetLineDash(context, 0, dashArray, 2);
CGContextAddArc(context, CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds), self.circleRadius, 0, 2 * M_PI, YES);
CGContextClosePath(context);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextDrawPath(context, kCGPathStroke);
}
- (void)drawGradientInContext:(CGContextRef)context
{
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [UIScreen mainScreen].scale;
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations);
CGPoint gradientCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
CGContextDrawRadialGradient(context,
gradient,
gradientCenter,
self.circleRadius - 1,
gradientCenter,
self.circleRadius + SHADOW_WIDTH,
kCGGradientDrawsAfterEndLocation);
CGColorSpaceRelease(colorspace);
CGGradientRelease(gradient);
}
Well, I need to apply zoom.
- (void)zoom:(UIPinchGestureRecognizer *)sender
{
effectiveScale = beginGestureScale * sender.scale;
self.circleRadius = effectiveScale * BASE_RADIUS;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
beginGestureScale = effectiveScale ? effectiveScale : 1.0;
}
return YES;
}
Well, since that things are screwed up: it glitches as the hell!
What could I do there?

Drawing MKMapView Overlay like Google Maps Directions

iOS 5 changed the way the built-in Google Maps App draws routes:
I would now like to replicate the design of the route overlay in my own app but I am currently only able to draw a plain blue line. I would like to add the 3D-effect with the gradient, borders and the glow. Any ideas on how to accomplish this?
Currently I'm using the following code:
CGContextSetFillColorWithColor(context, fillColor.CGColor);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, lineWidth);
CGContextAddPath(context, path);
CGContextReplacePathWithStrokedPath(context);
CGContextFillPath(context);
Resulting in a rather ugly line:
Thanks!
Update: The solution should work on iOS 4.0 and up.
I think that #ChrisMiles is correct in that the segments are probably being drawn individually. (I initially thought that this might have been doable using CGPatternRef but you don't have any access to the CTM or path endpoints inside the pattern drawing callback.)
With this in mind, here is an exceedingly crude, back-of-the-envelope example of how you might begin such an effort (filling the segments individually). Note that:
gradient colors are guessed
end caps are nonexistent and will need to be separately implemented
some aliasing artifacts remain
not a great deal of attention was paid to performance
Hopefully this can get you started at least (and works through some of the analytic geometry).
- (CGGradientRef)lineGradient
{
static CGGradientRef gradient = NULL;
if (gradient == NULL) {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef white = [[UIColor colorWithWhite:1.f
alpha:0.7f] CGColor];
CGColorRef blue = [[UIColor colorWithRed:0.1f
green:0.2f
blue:1.f
alpha:0.7f] CGColor];
CGColorRef lightBlue = [[UIColor colorWithRed:0.4f
green:0.6f
blue:1.f
alpha:0.7f] CGColor];
CFMutableArrayRef colors = CFArrayCreateMutable(kCFAllocatorDefault,
8,
NULL);
CFArrayAppendValue(colors, blue);
CFArrayAppendValue(colors, blue);
CFArrayAppendValue(colors, white);
CFArrayAppendValue(colors, white);
CFArrayAppendValue(colors, lightBlue);
CFArrayAppendValue(colors, lightBlue);
CFArrayAppendValue(colors, blue);
CFArrayAppendValue(colors, blue);
CGFloat locations[8] = {0.f, 0.08f, 0.14f, 0.21f, 0.29f, 0.86f, 0.93f, 1.f};
gradient = CGGradientCreateWithColors(colorSpace,
colors,
locations);
CFRelease(colors);
CGColorSpaceRelease(colorSpace);
}
return gradient;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetShouldAntialias(context, YES);
// Fill background color
[[UIColor whiteColor] setFill];
UIRectFill(rect);
// Build a path
CGFloat strokeWidth = 10.f;
CGContextSetLineWidth(context, strokeWidth);
CGGradientRef gradient = [self lineGradient];
CGPoint points[9] = {
CGPointMake(10.f, 25.f),
CGPointMake(100.f, 100.f),
CGPointMake(100.f, 150.f),
CGPointMake(22.f, 300.f),
CGPointMake(230.f, 400.f),
CGPointMake(230.f, 200.f),
CGPointMake(300.f, 200.f),
CGPointMake(310.f, 160.f),
CGPointMake(280.f, 100.f)
};
for (NSUInteger i = 1; i < 9; i++) {
CGPoint start = points[i - 1];
CGPoint end = points[i];
CGFloat dy = end.y - start.y;
CGFloat dx = end.x - start.x;
CGFloat xOffset, yOffset;
// Remember that, unlike Cartesian geometry, origin is in *upper* left!
if (dx == 0) {
// Vertical to start, gradient is horizontal
xOffset = 0.5 * strokeWidth;
yOffset = 0.f;
if (dy < 0) {
xOffset *= -1;
}
}
else if (dy == 0) {
// Horizontal to start, gradient is vertical
xOffset = 0.f;
yOffset = 0.5 * strokeWidth;
}
else {
// Sloped
CGFloat gradientSlope = - dx / dy;
xOffset = 0.5 * strokeWidth / sqrt(1 + gradientSlope * gradientSlope);
yOffset = 0.5 * strokeWidth / sqrt(1 + 1 / (gradientSlope * gradientSlope));
if (dx < 0 && dy > 0) {
yOffset *= -1;
}
else if (dx > 0 && dy < 0) {
xOffset *= -1;
}
else if (dx < 0 && dy < 0) {
yOffset *= -1;
xOffset *= -1;
}
else {
}
}
CGAffineTransform startTransform = CGAffineTransformMakeTranslation(-xOffset, yOffset);
CGAffineTransform endTransform = CGAffineTransformMakeTranslation(xOffset, -yOffset);
CGPoint gradientStart = CGPointApplyAffineTransform(start, startTransform);
CGPoint gradientEnd = CGPointApplyAffineTransform(start, endTransform);
CGContextSaveGState(context);
CGContextMoveToPoint(context, start.x, start.y);
CGContextAddLineToPoint(context, end.x, end.y);
CGContextReplacePathWithStrokedPath(context);
CGContextClip(context);
CGContextDrawLinearGradient(context,
gradient,
gradientStart,
gradientEnd,
kCGGradientDrawsAfterEndLocation | kCGGradientDrawsBeforeStartLocation);
CGContextRestoreGState(context);
}
CGContextRestoreGState(context);
}
I would say they are drawing a CGPath around the original line, stroking the edges and gradient filling it. The ends are capped by adding a semi circle to the CGPath.
Would be a bit more work than simply drawing a single line and stroking it, but gives them full control over the style of the rendered path.

Resources