

Nextrek
@ 67
Nextrek / iOS / Emmanuele Rossi / Mostri / Mostri / VC / ImageMaskView.mm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
// // Scratch and See // // The project provides en effect when the user swipes the finger over one texture // and by swiping reveals the texture underneath it. The effect can be applied for // scratch-card action or wiping a misted glass. // // Copyright (C) 2012 http://moqod.com Andrew Kopanev <andrew@moqod.com> // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies // of the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // #import "ImageMaskView.h" #import "PointTransforms.h" #import "Matrix.h" enum{ radius = 40 }; typedef void (*FillTileWithPointFunc)( id, SEL, CGPoint ); typedef void (*FillTileWithTwoPointsFunc)(id, SEL, CGPoint, CGPoint); @interface ImageMaskView() - (UIImage *)addTouches:(NSSet *)touches; - (void)fillTileWithPoint:(CGPoint) point; - (void)fillTileWithTwoPoints:(CGPoint)begin end:(CGPoint)end; @property (nonatomic) int tilesFilled; @property (nonatomic,retain) Matrix *maskedMatrix; @property (nonatomic) CGContextRef imageContext; @property (nonatomic) CGColorSpaceRef colorSpace; @end @implementation ImageMaskView @synthesize imageMaskFilledDelegate; @synthesize tilesFilled; @synthesize maskedMatrix; @synthesize imageContext,colorSpace; #pragma mark - memory management - (void)dealloc { self.maskedMatrix = nil; CGColorSpaceRelease(self.colorSpace); CGContextRelease(self.imageContext); [super dealloc]; } #pragma mark - - (id)initWithFrame:(CGRect)frame image:(UIImage *)img { if (self = [super initWithFrame:frame]) { // Initialization code self.userInteractionEnabled = YES; self.backgroundColor = [UIColor clearColor]; self.imageMaskFilledDelegate = nil; self.image = img; CGSize size = self.image.size; // initalize bitmap context self.colorSpace = CGColorSpaceCreateDeviceRGB(); self.imageContext = CGBitmapContextCreate(0,size.width, size.height, 8, size.width*4, colorSpace, kCGImageAlphaPremultipliedLast ); CGContextDrawImage(self.imageContext, CGRectMake(0, 0, size.width, size.height), self.image.CGImage); int blendMode = kCGBlendModeClear; CGContextSetBlendMode(self.imageContext, (CGBlendMode) blendMode); tilesX = size.width / (2 * radius); tilesY = size.height / (2 * radius); self.maskedMatrix = [[[Matrix alloc] initWithMax:MySizeMake(tilesX, tilesY)] autorelease]; self.tilesFilled = 0; } return self; } #pragma mark - - (double)procentsOfImageMasked { return 100.0 * self.tilesFilled / (self.maskedMatrix.max.x * self.maskedMatrix.max.y); } #pragma mark - UIResponder - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ self.image = [self addTouches:touches]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ self.image = [self addTouches:touches]; } #pragma mark - - (UIImage *)addTouches:(NSSet *)touches{ CGSize size = self.image.size; CGContextRef ctx = self.imageContext; CGContextSetFillColorWithColor(ctx,[UIColor clearColor].CGColor); CGContextSetStrokeColorWithColor(ctx,[UIColor colorWithRed:0 green:0 blue:0 alpha:0].CGColor); int tempFilled = self.tilesFilled; // process touches for (UITouch *touch in touches) { CGContextBeginPath(ctx); CGRect rect = {[touch locationInView:self], {4*radius, 4*radius}}; rect.origin = fromUItoQuartz(rect.origin, self.bounds.size); if(UITouchPhaseBegan == touch.phase){ // on begin, we just draw ellipse rect.origin.y -= radius; rect.origin.x -= radius; rect.origin = scalePoint(rect.origin, self.bounds.size, size); CGContextAddEllipseInRect(ctx, rect); CGContextFillPath(ctx); static const FillTileWithPointFunc fillTileFunc = (FillTileWithPointFunc) [self methodForSelector:@selector(fillTileWithPoint:)]; (*fillTileFunc)(self,@selector(fillTileWithPoint:),rect.origin); } else if(UITouchPhaseMoved == touch.phase) { // then touch moved, we draw superior-width line rect.origin = scalePoint(rect.origin, self.bounds.size, size); CGPoint prevPoint = [touch previousLocationInView:self]; prevPoint = fromUItoQuartz(prevPoint, self.bounds.size); prevPoint = scalePoint(prevPoint, self.bounds.size, size); CGContextSetStrokeColor(ctx,CGColorGetComponents([UIColor yellowColor].CGColor)); CGContextSetLineCap(ctx, kCGLineCapRound); CGContextSetLineWidth(ctx, 4*radius); CGContextMoveToPoint(ctx, prevPoint.x, prevPoint.y); CGContextAddLineToPoint(ctx, rect.origin.x, rect.origin.y); CGContextStrokePath(ctx); static const FillTileWithTwoPointsFunc fillTileFunc = (FillTileWithTwoPointsFunc) [self methodForSelector:@selector(fillTileWithTwoPoints:end:)]; (*fillTileFunc)(self,@selector(fillTileWithTwoPoints:end:),rect.origin, prevPoint); } } // was tilesFilled changed? if(tempFilled != self.tilesFilled){ [self.imageMaskFilledDelegate imageMaskView:self cleatPercentWasChanged:[self procentsOfImageMasked]]; } CGImageRef cgImage = CGBitmapContextCreateImage(ctx); UIImage *image = [UIImage imageWithCGImage:cgImage]; CGImageRelease(cgImage); return image; } /* * filling tile with one ellipse */ -(void)fillTileWithPoint:(CGPoint) point{ size_t x,y; x = point.x * self.maskedMatrix.max.x / self.image.size.width; y = point.y * self.maskedMatrix.max.y / self.image.size.height; char value = [self.maskedMatrix valueForCoordinates:x y:y]; if(!value){ [self.maskedMatrix setValue:1 forCoordinates:x y:y]; self.tilesFilled++; } } /* * filling tile with line */ -(void)fillTileWithTwoPoints:(CGPoint)begin end:(CGPoint)end{ CGFloat incrementerForx,incrementerFory; static const FillTileWithPointFunc fillTileFunc = (FillTileWithPointFunc) [self methodForSelector:@selector(fillTileWithPoint:)]; /* incrementers - about size of a tile */ incrementerForx = (begin.x < end.x ? 1 : -1) * self.image.size.width / tilesX; incrementerFory = (begin.y < end.y ? 1 : -1) * self.image.size.height / tilesY; // iterate on points between begin and end CGPoint i = begin; while(i.x <= end.x && i.y <= end.y){ (*fillTileFunc)(self,@selector(fillTileWithPoint:),i); i.x += incrementerForx; i.y += incrementerFory; } (*fillTileFunc)(self,@selector(fillTileWithPoint:),end); } @end |
Commits for Nextrek/iOS/Emmanuele Rossi/Mostri/Mostri/VC/ImageMaskView.mm
Revision | Author | Commited | Message |
---|---|---|---|
67 |
![]() |
Thu 23 Jan, 2014 14:31:55 +0000 | Emmanuele Rossi apps |