static const CGFloat kPadding = 10;
+@interface OverlayView()
+@property (nonatomic,assign) UIButton *cancelButton;
+@end
+
+
@implementation OverlayView
@synthesize delegate, oneDMode;
@synthesize points = _points;
-
+@synthesize cancelButton;
+@synthesize cropRect;
////////////////////////////////////////////////////////////////////////////////////////////////////
- (id) initWithFrame:(CGRect)theFrame cancelEnabled:(BOOL)isCancelEnabled oneDMode:(BOOL)isOneDModeEnabled {
- if( self = [super initWithFrame:theFrame] ) {
- self.backgroundColor = [UIColor clearColor];
+ if( self = [super initWithFrame:theFrame] ) {
+
+ CGFloat rectSize = self.frame.size.width - kPadding * 2;
+ if (!oneDMode) {
+ cropRect = CGRectMake(kPadding, (self.frame.size.height - rectSize) / 2, rectSize, rectSize);
+ } else {
+ CGFloat rectSize2 = self.frame.size.height - kPadding * 2;
+ cropRect = CGRectMake(kPadding, kPadding, rectSize, rectSize2);
+ }
+
+ self.backgroundColor = [UIColor clearColor];
self.oneDMode = isOneDModeEnabled;
if (isCancelEnabled) {
- cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
+ UIButton *butt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
+ self.cancelButton = butt;
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
if (oneDMode) {
[cancelButton setTransform:CGAffineTransformMakeRotation(M_PI/2)];
[cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:cancelButton];
+ [self addSubview:imageView];
}
}
- return self;
+ return self;
}
- (void)cancel:(id)sender {
////////////////////////////////////////////////////////////////////////////////////////////////////
- (void) dealloc {
[imageView release];
- imageView = nil;
[_points release];
- _points = nil;
- [cancelButton release];
[super dealloc];
}
CGContextStrokePath(context);
}
+- (CGPoint)map:(CGPoint)point {
+ CGPoint center;
+ center.x = cropRect.size.width/2;
+ center.y = cropRect.size.height/2;
+ float x = point.x - center.x;
+ float y = point.y - center.y;
+ int rotation = 90;
+ switch(rotation) {
+ case 0:
+ point.x = x;
+ point.y = y;
+ break;
+ case 90:
+ point.x = -y;
+ point.y = x;
+ break;
+ case 180:
+ point.x = -x;
+ point.y = -y;
+ break;
+ case 270:
+ point.x = y;
+ point.y = -x;
+ break;
+ }
+ point.x = point.x + center.x;
+ point.y = point.y + center.y;
+ return point;
+}
////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef c = UIGraphicsGetCurrentContext();
- CGRect cropRect = [self cropRect];
-
if (nil != _points) {
// [imageView.image drawAtPoint:cropRect.origin];
}
CGContextSetStrokeColor(c, blue);
CGContextSetFillColor(c, blue);
if (oneDMode) {
- CGPoint val1 = [[_points objectAtIndex:0] CGPointValue];
- CGPoint val2 = [[_points objectAtIndex:1] CGPointValue];
+ CGPoint val1 = [self map:[[_points objectAtIndex:0] CGPointValue]];
+ CGPoint val2 = [self map:[[_points objectAtIndex:1] CGPointValue]];
CGContextMoveToPoint(c, offset, val1.x);
CGContextAddLineToPoint(c, offset, val2.x);
CGContextStrokePath(c);
else {
CGRect smallSquare = CGRectMake(0, 0, 10, 10);
for( NSValue* value in _points ) {
- CGPoint point = [value CGPointValue];
+ CGPoint point = [self map:[value CGPointValue]];
smallSquare.origin = CGPointMake(
cropRect.origin.x + point.x - smallSquare.size.width / 2,
cropRect.origin.y + point.y - smallSquare.size.height / 2);
////////////////////////////////////////////////////////////////////////////////////////////////////
/*
- (void) setImage:(UIImage*)image {
- if( nil == _imageView ) {
- _imageView = [[UIImageView alloc] initWithImage:image];
- _imageView.alpha = 0.5;
- } else {
- _imageView.image = image;
- }
+ //if( nil == imageView ) {
+// imageView = [[UIImageView alloc] initWithImage:image];
+// imageView.alpha = 0.5;
+// } else {
+ imageView.image = image;
+ //}
- CGRect frame = _imageView.frame;
- frame.origin.x = self.cropRect.origin.x;
- frame.origin.y = self.cropRect.origin.y;
- _imageView.frame = frame;
+ //CGRect frame = imageView.frame;
+ //frame.origin.x = self.cropRect.origin.x;
+ //frame.origin.y = self.cropRect.origin.y;
+ //imageView.frame = CGRectMake(0,0, 30, 50);
- [_points release];
- _points = nil;
- self.backgroundColor = [UIColor clearColor];
+ //[_points release];
+ //_points = nil;
+ //self.backgroundColor = [UIColor clearColor];
- [self setNeedsDisplay];
+ //[self setNeedsDisplay];
}
*/
////////////////////////////////////////////////////////////////////////////////////////////////////
-- (CGRect) cropRect {
- CGFloat rectSize = self.frame.size.width - kPadding * 2;
- if (!oneDMode) {
- return CGRectMake(kPadding, (self.frame.size.height - rectSize) / 2, rectSize, rectSize);
- }
- else {
- CGFloat rectSize2 = self.frame.size.height - kPadding * 2;
- return CGRectMake(kPadding, kPadding, rectSize, rectSize2);
- }
+- (void) setPoints:(NSMutableArray*)pnts {
+ [pnts retain];
+ [_points release];
+ _points = pnts;
+
+ if (pnts != nil) {
+ self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.25];
+ }
+ [self setNeedsDisplay];
}
-
-////////////////////////////////////////////////////////////////////////////////////////////////////
-- (void) setPoints:(NSArray*)pnts {
- [pnts retain];
- [_points release];
- _points = pnts;
-
- if (pnts != nil) {
- self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.25];
- }
- [self setNeedsDisplay];
+- (void) setPoint:(CGPoint)point {
+ if (!_points) {
+ _points = [[NSMutableArray alloc] init];
+ }
+ if (_points.count > 3) {
+ [_points removeObjectAtIndex:0];
+ }
+ [_points addObject:[NSValue valueWithCGPoint:point]];
+ [self setNeedsDisplay];
}