Skip to content

Commit

Permalink
tiger example now utilise threading for drawing process
Browse files Browse the repository at this point in the history
  • Loading branch information
ap4y committed Jan 3, 2013
1 parent 531e6ba commit a69b2c9
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions example/svg_test/TigerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "TigerView.h"
#import "UIBezierPath+SVG.h"
#import <QuartzCore/QuartzCore.h>

@interface TigerView () {
NSString *_tigerPathes;
Expand Down Expand Up @@ -83,29 +84,39 @@ + (CGFloat)colorComponentFrom:(NSString *)string start:(NSUInteger)start length:
/* end of snippet */

- (void)drawRect:(CGRect)rect {
CGContextRef aRef = UIGraphicsGetCurrentContext();
CGContextScaleCTM(aRef, 1.0f, 1.0f);
CGContextTranslateCTM(aRef, 190.0f, 150.0f);

NSRegularExpression *pathObjectsRegex = [NSRegularExpression regularExpressionWithPattern:@"\\{.*?\\}"
options:0
error:nil];
NSArray *matches = [pathObjectsRegex matchesInString:_tigerPathes
options:0
range:NSMakeRange(0, _tigerPathes.length)];

for (NSTextCheckingResult *checkingResult in matches) {
NSString *pathObject = [_tigerPathes substringWithRange:checkingResult.range];
UIBezierPath *aPath = [self pathFromPathObject:pathObject];

aPath.lineWidth = [self strokeWidthFromPathObject:pathObject];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
CGContextRef aRef = UIGraphicsGetCurrentContext();
CGContextScaleCTM(aRef, 1.0f, 1.0f);
CGContextTranslateCTM(aRef, 190.0f, 150.0f);

[[self strokeColorFromPathObject:pathObject] setStroke];
[aPath stroke];
NSRegularExpression *pathObjectsRegex = [NSRegularExpression regularExpressionWithPattern:@"\\{.*?\\}"
options:0
error:nil];
NSArray *matches = [pathObjectsRegex matchesInString:_tigerPathes
options:0
range:NSMakeRange(0, _tigerPathes.length)];

[[self fillColorFromPathObject:pathObject] setFill];
[aPath fill];
}
for (NSTextCheckingResult *checkingResult in matches) {
NSString *pathObject = [_tigerPathes substringWithRange:checkingResult.range];
UIBezierPath *aPath = [self pathFromPathObject:pathObject];

aPath.lineWidth = [self strokeWidthFromPathObject:pathObject];

[[self strokeColorFromPathObject:pathObject] setStroke];
[aPath stroke];

[[self fillColorFromPathObject:pathObject] setFill];
[aPath fill];

UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
self.layer.contents = (id)[resultImage CGImage];
});
}

UIGraphicsEndImageContext();
});
}

- (UIBezierPath *)pathFromPathObject:(NSString *)pathObject {
Expand Down

0 comments on commit a69b2c9

Please sign in to comment.