From f56b2021e0313560e6cf0758121493c2939e436b Mon Sep 17 00:00:00 2001 From: ipatch Date: Sat, 14 Nov 2015 16:53:05 -0600 Subject: [PATCH] trying to crop avatars properly --- Classes/Controllers/ViewControllerAvatar4.m | 76 ++++++++++++++++++- .../xcshareddata/xcschemes/KegCop.xcscheme | 2 +- TODO | 4 +- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/Classes/Controllers/ViewControllerAvatar4.m b/Classes/Controllers/ViewControllerAvatar4.m index 446dbe77..294487a9 100644 --- a/Classes/Controllers/ViewControllerAvatar4.m +++ b/Classes/Controllers/ViewControllerAvatar4.m @@ -18,6 +18,8 @@ @interface ViewControllerAvatar4 () @property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput; @property(nonatomic, retain) IBOutlet UIImageView *vImage; @property(nonatomic, retain) NSData *dataImage; +@property(nonatomic, retain) UIImage *image; +@property(nonatomic, retain) UIImage *croppedImage; // Core Data @property(nonatomic, retain) NSManagedObjectContext *managedObjectContext; @@ -82,6 +84,20 @@ -(void)viewDidLoad { [session addInput:input]; _stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; + + // add Dictionary to store PixelXDimension / PixelYDimension + double width = 480; + double height = 480; + NSDictionary *pixelBufferOptions = [NSDictionary dictionaryWithObjectsAndKeys: + [NSNumber numberWithDouble:width], (id)kCVPixelBufferWidthKey, + [NSNumber numberWithDouble:height], (id)kCVPixelBufferHeightKey, + [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey, + nil]; + +// NSDictionary *options = @{(id)kCVPixelBufferPixelFormatTypeKey : kCVPixelFormatType_32BGRA, +// (id)kCVPixelBufferWidthKey : width, +// (id)kCVPixelBufferHeightKey : height }; + NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil]; [_stillImageOutput setOutputSettings:outputSettings]; [session addOutput:_stillImageOutput]; @@ -156,16 +172,21 @@ -(IBAction)captureNow { } NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; - UIImage *image = [[UIImage alloc] initWithData:imageData]; + _image = [[UIImage alloc] initWithData:imageData]; - self.vImage.image = image; + self.vImage.image = _image; // the below line will save the image to the PhotoAlbum // UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); + float width = 360; + float height = 360; + CGSize squareSize = CGSizeMake(width, height); + + [self squareImageWithImage:_image scaledToSize:squareSize]; + + _dataImage = UIImageJPEGRepresentation(_image, 0.0); - _dataImage = UIImageJPEGRepresentation(image, 0.0); - // figure out how to save pic for a particular account entity // save to Core Data moc // save picture / avatar to CoreData entity specific to username @@ -173,6 +194,53 @@ -(IBAction)captureNow { }]; } + +#pragma mark - Square Image With Image +- (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { + + // see http://stackoverflow.com/questions/158914/cropping-an-uiimage + // see http://stackoverflow.com/a/28085176/708807 + + double ratio; + double delta; + CGPoint offset; + + //make a new square size, that is the resized imaged width + CGSize sz = CGSizeMake(newSize.width, newSize.width); + + //figure out if the picture is landscape or portrait, then + //calculate scale factor and offset + if (image.size.width > image.size.height) { + ratio = newSize.width / image.size.width; + delta = (ratio*image.size.width - ratio*image.size.height); + offset = CGPointMake(delta/2, 0); + } else { + ratio = newSize.width / image.size.height; + delta = (ratio*image.size.height - ratio*image.size.width); + offset = CGPointMake(0, delta/2); + } + + //make the final clipping rect based on the calculated values + CGRect clipRect = CGRectMake(-offset.x, -offset.y, + (ratio * image.size.width) + delta, + (ratio * image.size.height) + delta); + + + //start a new context, with scale factor 0.0 so retina displays get + //high quality image + if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { + UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0); + } else { + UIGraphicsBeginImageContext(sz); + } + UIRectClip(clipRect); + [image drawInRect:clipRect]; + UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + return newImage; +} + # pragma mark - save Avatar -(void)saveAvatar { #ifdef DEBUG diff --git a/KegCop.xcworkspace/xcshareddata/xcschemes/KegCop.xcscheme b/KegCop.xcworkspace/xcshareddata/xcschemes/KegCop.xcscheme index 65684d84..917744a4 100644 --- a/KegCop.xcworkspace/xcshareddata/xcschemes/KegCop.xcscheme +++ b/KegCop.xcworkspace/xcshareddata/xcschemes/KegCop.xcscheme @@ -56,7 +56,7 @@