Domanda

I'm trying to draw a rough line, a normal straight line just looks odd in my app. I'm using a fun font etc and a straight line just doesn't work.

When I say rough, I mean I want to add some noise or texture to it.

I've tried adding an image as a texture but the options I've tried use a uniform tiled approach which looks too georometricly sound.

I need to either add a scatter effect to draw my texture image roughly in the shape of a line or some sort of random approach to add noise.

I know I could just draw a rough line in a imaging app and use that, but I'm not sure about colouring and size at this stage and I'd rather use something dynamic.

I've spend far too long on this issue and a lot if time googling and can't find anything I can use, so if you have a solution please provide or point me at some example code.

Thanks.

This is the best solution I've found, but it's too uniform...

#import "DrawTexturedLine.h"
@implementation DrawTexturedLine

- (void)drawRect:(CGRect)rect
{    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self patternMake2:rect context:context];
}

void pattern2Callback (void *info, CGContextRef context) {
    UIImage *image = [UIImage imageNamed:@"particle.png"];
    CGImageRef imageRef = [image CGImage];
    CGContextDrawImage(context, CGRectMake(0, 0, 50, 25), imageRef);
}

- (void)patternMake2:(CGRect)rect context:(CGContextRef)context
{
    static const CGPatternCallbacks callbacks = { 0, &pattern2Callback, NULL };

    CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL);
    CGContextSetFillColorSpace(context, patternSpace);
    CGColorSpaceRelease(patternSpace);
    CGSize patternSize = CGSizeMake(25/2.0, 25);
    CGPatternRef pattern = CGPatternCreate(NULL, self.bounds, 
        CGAffineTransformIdentity, patternSize.width, patternSize.height, 
        kCGPathFillStroke, true, &callbacks);
    CGFloat alpha = 1;
    CGContextSetFillPattern(context, pattern, &alpha);

    CGPatternRelease(pattern);
    CGContextFillRect(context, rect);
    //CGContextRestoreGState(context);
     }
@end
È stato utile?

Soluzione 2

I'm just returning to this issue and I've spent far too much time on this, looking at various tinting solutions. In the end I had to use a paint package to do most of the work. If I had been better with it I could have done the whole thing in a paint package.

I ended up drawing some lines with a black marker scanning them into gray scale then colorizing it.

Altri suggerimenti

Not quite answering your question but you could draw the lines in a paint package and include those with your app and then use code like this to set the colour of the lines.

https://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage

If you were to use Sprite Kit it becomes much easier to change colors of images

[SKAction colorizeWithColor:[self randomColor]
                             colorBlendFactor:1.0
                                     duration:0]];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top