+ Image Extraction from Annotation Controller is working

This commit is contained in:
moritz haarmann
2014-02-26 12:40:53 +01:00
parent 35dc6c58d5
commit 77bab0d73d
3 changed files with 146 additions and 13 deletions

View File

@@ -8,17 +8,49 @@
#import "BITArrowImageAnnotation.h"
#define kArrowPointCount 7
@interface BITArrowImageAnnotation()
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@end
@implementation BITArrowImageAnnotation
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.shapeLayer = [CAShapeLayer layer];
self.shapeLayer.strokeColor = [UIColor redColor].CGColor;
self.shapeLayer.lineWidth = 5;
self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
[self.layer addSublayer:self.shapeLayer];
}
return self;
}
- (void)buildShape {
CGFloat topHeight = MAX(self.frame.size.width / 3.0f,20);
CGFloat lineWidth = MAX(self.frame.size.width / 5.0f,20);
UIBezierPath *path = [self bezierPathWithArrowFromPoint:CGPointMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame)) toPoint:CGPointMake(CGRectGetMaxX(self.frame), CGRectGetMaxY(self.frame)) tailWidth:lineWidth headWidth:self.frame.size.height headLength:topHeight];
self.shapeLayer.path = path.CGPath;
}
-(void)layoutSubviews{
[super layoutSubviews];
[self buildShape];
self.shapeLayer.frame = self.bounds;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
@@ -28,4 +60,62 @@
}
*/
- (UIBezierPath *)bezierPathWithArrowFromPoint:(CGPoint)startPoint
toPoint:(CGPoint)endPoint
tailWidth:(CGFloat)tailWidth
headWidth:(CGFloat)headWidth
headLength:(CGFloat)headLength {
CGFloat length = hypotf(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
CGPoint points[kArrowPointCount];
[self getAxisAlignedArrowPoints:points
forLength:length
tailWidth:tailWidth
headWidth:headWidth
headLength:headLength];
CGAffineTransform transform = [self transformForStartPoint:startPoint
endPoint:endPoint
length:length];
CGMutablePathRef cgPath = CGPathCreateMutable();
CGPathAddLines(cgPath, &transform, points, sizeof points / sizeof *points);
CGPathCloseSubpath(cgPath);
UIBezierPath *uiPath = [UIBezierPath bezierPathWithCGPath:cgPath];
CGPathRelease(cgPath);
return uiPath;
}
- (void)getAxisAlignedArrowPoints:(CGPoint[kArrowPointCount])points
forLength:(CGFloat)length
tailWidth:(CGFloat)tailWidth
headWidth:(CGFloat)headWidth
headLength:(CGFloat)headLength {
CGFloat tailLength = length - headLength;
points[0] = CGPointMake(0, tailWidth / 2);
points[1] = CGPointMake(tailLength, tailWidth / 2);
points[2] = CGPointMake(tailLength, headWidth / 2);
points[3] = CGPointMake(length, 0);
points[4] = CGPointMake(tailLength, -headWidth / 2);
points[5] = CGPointMake(tailLength, -tailWidth / 2);
points[6] = CGPointMake(0, -tailWidth / 2);
}
+ (CGAffineTransform)dqd_transformForStartPoint:(CGPoint)startPoint
endPoint:(CGPoint)endPoint
length:(CGFloat)length {
CGFloat cosine = (endPoint.x - startPoint.x) / length;
CGFloat sine = (endPoint.y - startPoint.y) / length;
return (CGAffineTransform){ cosine, sine, -sine, cosine, startPoint.x, startPoint.y };
}
- (CGAffineTransform)transformForStartPoint:(CGPoint)startPoint
endPoint:(CGPoint)endPoint
length:(CGFloat)length {
CGFloat cosine = (endPoint.x - startPoint.x) / length;
CGFloat sine = (endPoint.y - startPoint.y) / length;
return (CGAffineTransform){ cosine, sine, -sine, cosine, startPoint.x, startPoint.y };
}
@end

View File

@@ -9,6 +9,7 @@
#import "BITImageAnnotationViewController.h"
#import "BITImageAnnotation.h"
#import "BITRectangleImageAnnotation.h"
#import "BITArrowImageAnnotation.h"
@interface BITImageAnnotationViewController ()
@@ -17,6 +18,7 @@
@property (nonatomic, strong) NSMutableArray *objects;
@property (nonatomic, strong) UIPanGestureRecognizer *panRecognizer;
@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;
@property (nonatomic) CGFloat scaleFactor;
@property (nonatomic) CGPoint panStart;
@property (nonatomic,strong) BITImageAnnotation *currentAnnotation;
@@ -38,33 +40,55 @@
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.editingControls = [[UISegmentedControl alloc] initWithItems:@[@"Arrow", @"Rect", @"Blur"]];
self.navigationItem.titleView = self.editingControls;
self.objects = [NSMutableArray new];
[self.editingControls addTarget:self action:@selector(editingAction:) forControlEvents:UIControlEventTouchUpInside];
self.imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
self.imageView.clipsToBounds = YES;
self.imageView.layer.masksToBounds = YES;
self.imageView.image = self.image;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.contentMode = UIViewContentModeScaleToFill;
[self.view addSubview:self.imageView];
self.imageView.frame = self.view.bounds;
self.tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
self.panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panned:)];
[self.tapRecognizer requireGestureRecognizerToFail:self.panRecognizer];
[self.view addGestureRecognizer:self.tapRecognizer];
[self.view addGestureRecognizer:self.panRecognizer];
[self.imageView addGestureRecognizer:self.tapRecognizer];
[self.imageView addGestureRecognizer:self.panRecognizer];
self.imageView.userInteractionEnabled = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc ] initWithTitle:@"Discard" style:UIBarButtonItemStyleBordered target:self action:@selector(discard:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc ] initWithTitle:@"Save" style:UIBarButtonItemStyleBordered target:self action:@selector(save:)];
CGFloat heightScaleFactor = self.view.frame.size.height / self.image.size.height;
CGFloat widthScaleFactor = self.view.frame.size.width / self.image.size.width;
CGFloat factor = MIN(heightScaleFactor, widthScaleFactor);
self.scaleFactor = factor;
CGSize scaledImageSize = CGSizeMake(self.image.size.width * factor, self.image.size.height * factor);
self.imageView.frame = CGRectMake(self.view.frame.size.width/2 - scaledImageSize.width/2, self.view.frame.size.height/2 - scaledImageSize.height/2, scaledImageSize.width, scaledImageSize.height);
// Do any additional setup after loading the view.
}
@@ -75,6 +99,8 @@
- (BITImageAnnotation *)annotationForCurrentMode {
if (self.editingControls.selectedSegmentIndex == 0){
return [[BITRectangleImageAnnotation alloc] initWithFrame:CGRectZero];
} else if(self.editingControls.selectedSegmentIndex==1){
return [[BITArrowImageAnnotation alloc] initWithFrame:CGRectZero];
} else {
return [[BITImageAnnotation alloc] initWithFrame:CGRectZero];
}
@@ -94,9 +120,18 @@
}
- (UIImage *)extractImage {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0.0);
UIGraphicsBeginImageContextWithOptions(self.image.size, YES, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:ctx];
[self.image drawInRect:CGRectMake(0, 0, self.image.size.width, self.image.size.height)];
CGContextScaleCTM(ctx,1.0/self.scaleFactor,1.0f/self.scaleFactor);
// Drawing all the annotations onto the final image.
for (BITImageAnnotation *annotation in self.objects){
CGContextTranslateCTM(ctx, annotation.frame.origin.x, annotation.frame.origin.y);
[annotation.layer renderInContext:ctx];
CGContextTranslateCTM(ctx,-1 * annotation.frame.origin.x,-1 * annotation.frame.origin.y);
}
UIImage *renderedImageOfMyself = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return renderedImageOfMyself;
@@ -107,8 +142,8 @@
- (void)panned:(UIPanGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
self.currentAnnotation = [self annotationForCurrentMode];
[self.view insertSubview:self.currentAnnotation aboveSubview:self.imageView];
[self.objects addObject:self.currentAnnotation];
[self.imageView insertSubview:self.currentAnnotation aboveSubview:self.imageView];
self.panStart = [gestureRecognizer locationInView:self.imageView];
} else if (gestureRecognizer.state == UIGestureRecognizerStateChanged){
CGPoint bla = [gestureRecognizer translationInView:self.imageView];

View File

@@ -134,6 +134,8 @@
973EC8B418BCA7BC00DBFFBB /* BITImageAnnotationViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 97F0FA0018AE375E00EF50AA /* BITImageAnnotationViewController.m */; };
973EC8B718BCA8A200DBFFBB /* BITRectangleImageAnnotation.h in Headers */ = {isa = PBXBuildFile; fileRef = 973EC8B518BCA8A200DBFFBB /* BITRectangleImageAnnotation.h */; };
973EC8B818BCA8A200DBFFBB /* BITRectangleImageAnnotation.m in Sources */ = {isa = PBXBuildFile; fileRef = 973EC8B618BCA8A200DBFFBB /* BITRectangleImageAnnotation.m */; };
973EC8BB18BDE29800DBFFBB /* BITArrowImageAnnotation.h in Headers */ = {isa = PBXBuildFile; fileRef = 973EC8B918BDE29800DBFFBB /* BITArrowImageAnnotation.h */; };
973EC8BC18BDE29800DBFFBB /* BITArrowImageAnnotation.m in Sources */ = {isa = PBXBuildFile; fileRef = 973EC8BA18BDE29800DBFFBB /* BITArrowImageAnnotation.m */; };
9760F6C418BB4D2D00959B93 /* AssetsLibrary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9760F6C318BB4D2D00959B93 /* AssetsLibrary.framework */; };
9760F6CF18BB685600959B93 /* BITImageAnnotation.h in Headers */ = {isa = PBXBuildFile; fileRef = 9760F6CD18BB685600959B93 /* BITImageAnnotation.h */; };
9760F6D018BB685600959B93 /* BITImageAnnotation.m in Sources */ = {isa = PBXBuildFile; fileRef = 9760F6CE18BB685600959B93 /* BITImageAnnotation.m */; };
@@ -300,6 +302,8 @@
1EFF03E417F2485500A5F13C /* BITCrashManagerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BITCrashManagerTests.m; sourceTree = "<group>"; };
973EC8B518BCA8A200DBFFBB /* BITRectangleImageAnnotation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BITRectangleImageAnnotation.h; sourceTree = "<group>"; };
973EC8B618BCA8A200DBFFBB /* BITRectangleImageAnnotation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BITRectangleImageAnnotation.m; sourceTree = "<group>"; };
973EC8B918BDE29800DBFFBB /* BITArrowImageAnnotation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BITArrowImageAnnotation.h; sourceTree = "<group>"; };
973EC8BA18BDE29800DBFFBB /* BITArrowImageAnnotation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BITArrowImageAnnotation.m; sourceTree = "<group>"; };
9760F6C318BB4D2D00959B93 /* AssetsLibrary.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AssetsLibrary.framework; path = System/Library/Frameworks/AssetsLibrary.framework; sourceTree = SDKROOT; };
9760F6CD18BB685600959B93 /* BITImageAnnotation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BITImageAnnotation.h; sourceTree = "<group>"; };
9760F6CE18BB685600959B93 /* BITImageAnnotation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BITImageAnnotation.m; sourceTree = "<group>"; };
@@ -564,6 +568,8 @@
9760F6CE18BB685600959B93 /* BITImageAnnotation.m */,
973EC8B518BCA8A200DBFFBB /* BITRectangleImageAnnotation.h */,
973EC8B618BCA8A200DBFFBB /* BITRectangleImageAnnotation.m */,
973EC8B918BDE29800DBFFBB /* BITArrowImageAnnotation.h */,
973EC8BA18BDE29800DBFFBB /* BITArrowImageAnnotation.m */,
);
name = "Image Editor";
sourceTree = "<group>";
@@ -699,6 +705,7 @@
1E49A4B5161222B900463151 /* BITHockeyBaseManagerPrivate.h in Headers */,
E4933E8017B66CDA00B11ACC /* BITHTTPOperation.h in Headers */,
1E49A4BE161222B900463151 /* BITHockeyHelper.h in Headers */,
973EC8BB18BDE29800DBFFBB /* BITArrowImageAnnotation.h in Headers */,
1E49A4C4161222B900463151 /* BITAppStoreHeader.h in Headers */,
1E49A4CA161222B900463151 /* BITStoreButton.h in Headers */,
973EC8B718BCA8A200DBFFBB /* BITRectangleImageAnnotation.h in Headers */,
@@ -940,6 +947,7 @@
1E754E611621FBB70070AB92 /* BITCrashReportTextFormatter.m in Sources */,
1EF95CA7162CB037000AE3AD /* BITFeedbackActivity.m in Sources */,
1EACC97C162F041E007578C5 /* BITAttributedLabel.m in Sources */,
973EC8BC18BDE29800DBFFBB /* BITArrowImageAnnotation.m in Sources */,
973EC8B418BCA7BC00DBFFBB /* BITImageAnnotationViewController.m in Sources */,
1E0FEE29173BDB260061331F /* BITKeychainUtils.m in Sources */,
1E94F9E216E91330006570AD /* BITStoreUpdateManager.m in Sources */,