From 0242f6d45cf4d82cb67f36ddda4daff722e29531 Mon Sep 17 00:00:00 2001 From: Jason Prasad Date: Sun, 25 May 2014 02:37:56 -0400 Subject: [PATCH 1/8] Checked for screen orientation --- MMLayershots/MMLayershots.m | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/MMLayershots/MMLayershots.m b/MMLayershots/MMLayershots.m index f958270..fbc2b51 100644 --- a/MMLayershots/MMLayershots.m +++ b/MMLayershots/MMLayershots.m @@ -71,9 +71,17 @@ - (void)userDidTakeScreenshot { - (NSData *)psdRepresentationForScreen:(UIScreen *)screen { // Initial setup - CGSize size = [UIScreen mainScreen].bounds.size; + CGSize size; + UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; + if (UIInterfaceOrientationIsPortrait(orientation)) + { + size = [UIScreen mainScreen].bounds.size; + } else { + size = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); + } size.width = size.width * [UIScreen mainScreen].scale; size.height = size.height * [UIScreen mainScreen].scale; + PSDWriter * psdWriter = [[PSDWriter alloc] initWithDocumentSize:size]; NSArray *allWindows; @@ -141,9 +149,20 @@ - (NSArray *)buildImagesForLayer:(CALayer *)layer rootLayer:(CALayer *)rootLayer } - (UIImage *)imageFromLayer:(CALayer *)layer { - UIGraphicsBeginImageContextWithOptions(layer.bounds.size, NO, [UIScreen mainScreen].scale); + CGSize size; + UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; + if (UIInterfaceOrientationIsPortrait(orientation)) + { + size = layer.bounds.size; + } else { + size = CGSizeMake(layer.bounds.size.height, layer.bounds.size.width); + } + + UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); CGContextRef ctx = UIGraphicsGetCurrentContext(); - [layer renderInContext:ctx]; + + [layer.sublayers[0] renderInContext:ctx]; + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; From 49d52e9d6d55b8eb7df82656098b3237ba54656f Mon Sep 17 00:00:00 2001 From: Jason Prasad Date: Sun, 25 May 2014 10:40:36 -0400 Subject: [PATCH 2/8] Added screen info as context --- MMLayershots/MMLayershots.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MMLayershots/MMLayershots.m b/MMLayershots/MMLayershots.m index fbc2b51..0872684 100644 --- a/MMLayershots/MMLayershots.m +++ b/MMLayershots/MMLayershots.m @@ -153,9 +153,9 @@ - (UIImage *)imageFromLayer:(CALayer *)layer { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (UIInterfaceOrientationIsPortrait(orientation)) { - size = layer.bounds.size; + size = [UIScreen mainScreen].bounds.size; } else { - size = CGSizeMake(layer.bounds.size.height, layer.bounds.size.width); + size = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); } UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); From 7ffe9ccb79d8923ed86cca036ec1a0468934fd9c Mon Sep 17 00:00:00 2001 From: Jason Prasad Date: Sun, 25 May 2014 10:53:40 -0400 Subject: [PATCH 3/8] Created size for orientation method --- MMLayershots/MMLayershots.m | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/MMLayershots/MMLayershots.m b/MMLayershots/MMLayershots.m index 0872684..2bdde23 100644 --- a/MMLayershots/MMLayershots.m +++ b/MMLayershots/MMLayershots.m @@ -71,14 +71,7 @@ - (void)userDidTakeScreenshot { - (NSData *)psdRepresentationForScreen:(UIScreen *)screen { // Initial setup - CGSize size; - UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; - if (UIInterfaceOrientationIsPortrait(orientation)) - { - size = [UIScreen mainScreen].bounds.size; - } else { - size = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); - } + CGSize size = [self sizeForDeviceOrientation]; size.width = size.width * [UIScreen mainScreen].scale; size.height = size.height * [UIScreen mainScreen].scale; @@ -149,14 +142,7 @@ - (NSArray *)buildImagesForLayer:(CALayer *)layer rootLayer:(CALayer *)rootLayer } - (UIImage *)imageFromLayer:(CALayer *)layer { - CGSize size; - UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; - if (UIInterfaceOrientationIsPortrait(orientation)) - { - size = [UIScreen mainScreen].bounds.size; - } else { - size = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); - } + CGSize size = [self sizeForDeviceOrientation]; UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); CGContextRef ctx = UIGraphicsGetCurrentContext(); @@ -168,4 +154,17 @@ - (UIImage *)imageFromLayer:(CALayer *)layer { return image; } +- (CGSize)sizeForDeviceOrientation { + CGSize size; + UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; + if (UIInterfaceOrientationIsPortrait(orientation)) + { + size = [UIScreen mainScreen].bounds.size; + } else { + size = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); + } + + return size; +} + @end From 1dfdaacf763371037d7659d0d3a5e2b28c9b3fc4 Mon Sep 17 00:00:00 2001 From: Jason Prasad Date: Sun, 25 May 2014 11:00:59 -0400 Subject: [PATCH 4/8] Modified .gitignore --- .gitignore | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 8005fbc..ffb6bf9 100644 --- a/.gitignore +++ b/.gitignore @@ -14,14 +14,8 @@ build/ xcuserdata profile *.moved-aside -DerivedData -.idea/ -# build artifacts -bin/ -code_coverage/ -test-reports/ -project_temp/ -.bundle/ + # CocoaPods Pods/ +.DS_Store From 4b0ad1510f477148eedc6584a9319349a04990ae Mon Sep 17 00:00:00 2001 From: Jason Prasad Date: Sun, 25 May 2014 11:12:45 -0400 Subject: [PATCH 5/8] Modified .gitignore again --- .gitignore | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitignore b/.gitignore index ffb6bf9..b877d4b 100644 --- a/.gitignore +++ b/.gitignore @@ -15,7 +15,4 @@ xcuserdata profile *.moved-aside -# CocoaPods -Pods/ - .DS_Store From c5b3941a8a3c7c8b3762dfc49f90b9b9544df667 Mon Sep 17 00:00:00 2001 From: Jason Prasad Date: Sun, 25 May 2014 14:34:14 -0400 Subject: [PATCH 6/8] Apply transforms to context only Changed sizeForDevice... to sizeForInterface (language) Added bool method --- MMLayershots/MMLayershots.m | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/MMLayershots/MMLayershots.m b/MMLayershots/MMLayershots.m index 2bdde23..0e17782 100644 --- a/MMLayershots/MMLayershots.m +++ b/MMLayershots/MMLayershots.m @@ -71,7 +71,7 @@ - (void)userDidTakeScreenshot { - (NSData *)psdRepresentationForScreen:(UIScreen *)screen { // Initial setup - CGSize size = [self sizeForDeviceOrientation]; + CGSize size = [self sizeForInterfaceOrientation]; size.width = size.width * [UIScreen mainScreen].scale; size.height = size.height * [UIScreen mainScreen].scale; @@ -142,23 +142,25 @@ - (NSArray *)buildImagesForLayer:(CALayer *)layer rootLayer:(CALayer *)rootLayer } - (UIImage *)imageFromLayer:(CALayer *)layer { - CGSize size = [self sizeForDeviceOrientation]; - + CGSize size = [self sizeForInterfaceOrientation]; UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); CGContextRef ctx = UIGraphicsGetCurrentContext(); + + // if interface is in landscape, apply transforms to context for layers that need transform + if (![self isInterfaceInPortrait] && CATransform3DIsIdentity(layer.transform)) { + CGContextTranslateCTM(ctx, 0, size.height); + CGContextRotateCTM(ctx, -M_PI_2); + } - [layer.sublayers[0] renderInContext:ctx]; - + [layer renderInContext:ctx]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } -- (CGSize)sizeForDeviceOrientation { +- (CGSize)sizeForInterfaceOrientation { CGSize size; - UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; - if (UIInterfaceOrientationIsPortrait(orientation)) - { + if ([self isInterfaceInPortrait]) { size = [UIScreen mainScreen].bounds.size; } else { size = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); @@ -167,4 +169,9 @@ - (CGSize)sizeForDeviceOrientation { return size; } +- (BOOL)isInterfaceInPortrait { + UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; + return UIInterfaceOrientationIsPortrait(orientation); +} + @end From e7d095b16780a1d2365b191e85a8139c8d8dfbe8 Mon Sep 17 00:00:00 2001 From: Jason Prasad Date: Sun, 25 May 2014 23:21:11 -0400 Subject: [PATCH 7/8] Checking for status bar delegate instead... Instead of checking for transform identity now check if the layer has the status bar as a delegate. --- MMLayershots/MMLayershots.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MMLayershots/MMLayershots.m b/MMLayershots/MMLayershots.m index 0e17782..eca083b 100644 --- a/MMLayershots/MMLayershots.m +++ b/MMLayershots/MMLayershots.m @@ -146,8 +146,8 @@ - (UIImage *)imageFromLayer:(CALayer *)layer { UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); CGContextRef ctx = UIGraphicsGetCurrentContext(); - // if interface is in landscape, apply transforms to context for layers that need transform - if (![self isInterfaceInPortrait] && CATransform3DIsIdentity(layer.transform)) { + // if interface is in landscape, apply transforms to context for layers not in the status bar + if (![self isInterfaceInPortrait] && ![layer.delegate isKindOfClass:NSClassFromString(@"UIStatusBarWindow")]) { CGContextTranslateCTM(ctx, 0, size.height); CGContextRotateCTM(ctx, -M_PI_2); } From 9bc878545e9d4f81f05236cad2db834d539719f6 Mon Sep 17 00:00:00 2001 From: Jason Prasad Date: Tue, 27 May 2014 20:33:53 -0400 Subject: [PATCH 8/8] Added applyTransformsToImage... --- MMLayershots/MMLayershots.m | 47 +++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/MMLayershots/MMLayershots.m b/MMLayershots/MMLayershots.m index eca083b..49ce356 100644 --- a/MMLayershots/MMLayershots.m +++ b/MMLayershots/MMLayershots.m @@ -71,7 +71,8 @@ - (void)userDidTakeScreenshot { - (NSData *)psdRepresentationForScreen:(UIScreen *)screen { // Initial setup - CGSize size = [self sizeForInterfaceOrientation]; + UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; + CGSize size = [self sizeForInterfaceOrientation:orientation]; size.width = size.width * [UIScreen mainScreen].scale; size.height = size.height * [UIScreen mainScreen].scale; @@ -142,25 +143,42 @@ - (NSArray *)buildImagesForLayer:(CALayer *)layer rootLayer:(CALayer *)rootLayer } - (UIImage *)imageFromLayer:(CALayer *)layer { - CGSize size = [self sizeForInterfaceOrientation]; - UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); + UIGraphicsBeginImageContextWithOptions(layer.bounds.size, NO, [UIScreen mainScreen].scale); CGContextRef ctx = UIGraphicsGetCurrentContext(); - - // if interface is in landscape, apply transforms to context for layers not in the status bar - if (![self isInterfaceInPortrait] && ![layer.delegate isKindOfClass:NSClassFromString(@"UIStatusBarWindow")]) { - CGContextTranslateCTM(ctx, 0, size.height); - CGContextRotateCTM(ctx, -M_PI_2); - } - [layer renderInContext:ctx]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); + + UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; + if (!UIInterfaceOrientationIsPortrait(orientation) && ![layer.delegate isKindOfClass:NSClassFromString(@"UIStatusBarWindow")]) { + image = [self applyTransformsToImage:image forInterfaceOrientation:orientation]; + } + return image; } -- (CGSize)sizeForInterfaceOrientation { +- (UIImage *)applyTransformsToImage:(UIImage *)image forInterfaceOrientation:(UIInterfaceOrientation)orientation { + CGSize size = [self sizeForInterfaceOrientation:orientation]; + UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); + CGContextRef context = UIGraphicsGetCurrentContext(); + + if (orientation == UIInterfaceOrientationLandscapeRight) { + CGContextTranslateCTM(context, 0, size.height); + CGContextRotateCTM (context, -M_PI_2); + } else if (orientation == UIInterfaceOrientationLandscapeLeft) { + CGContextTranslateCTM(context, size.width, 0); + CGContextRotateCTM (context, M_PI_2); + } + + [image drawAtPoint:CGPointMake(0, 0)]; + UIImage *transformedImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + return transformedImage; +} + +- (CGSize)sizeForInterfaceOrientation:(UIInterfaceOrientation)orientation { CGSize size; - if ([self isInterfaceInPortrait]) { + if (UIInterfaceOrientationIsPortrait(orientation)) { size = [UIScreen mainScreen].bounds.size; } else { size = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); @@ -169,9 +187,4 @@ - (CGSize)sizeForInterfaceOrientation { return size; } -- (BOOL)isInterfaceInPortrait { - UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; - return UIInterfaceOrientationIsPortrait(orientation); -} - @end