+ 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

@@ -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,32 +40,54 @@
{
[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];