Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves issue #5 #7

Merged
merged 9 commits into from
May 28, 2014
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,5 @@ build/
xcuserdata
profile
*.moved-aside
DerivedData
.idea/
# build artifacts
bin/
code_coverage/
test-reports/
project_temp/
.bundle/
# CocoaPods
Pods/

.DS_Store
29 changes: 27 additions & 2 deletions MMLayershots/MMLayershots.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ - (void)userDidTakeScreenshot {

- (NSData *)psdRepresentationForScreen:(UIScreen *)screen {
// Initial setup
CGSize size = [UIScreen mainScreen].bounds.size;
CGSize size = [self sizeForInterfaceOrientation];
size.width = size.width * [UIScreen mainScreen].scale;
size.height = size.height * [UIScreen mainScreen].scale;

PSDWriter * psdWriter = [[PSDWriter alloc] initWithDocumentSize:size];

NSArray *allWindows;
Expand Down Expand Up @@ -141,12 +142,36 @@ - (NSArray *)buildImagesForLayer:(CALayer *)layer rootLayer:(CALayer *)rootLayer
}

- (UIImage *)imageFromLayer:(CALayer *)layer {
UIGraphicsBeginImageContextWithOptions(layer.bounds.size, NO, [UIScreen mainScreen].scale);
CGSize size = [self sizeForInterfaceOrientation];
UIGraphicsBeginImageContextWithOptions(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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not quite right yet. This works for device orientation left, but fails if the device is rotated to the right. Since rotated the same way independent of the device orientation, it will show the UI upside down for one of the device rotations?

layershots_wrong_translation

Check for both orientations could be done like so:

    if (orientation==UIInterfaceOrientationLandscapeLeft) {
        CGContextTranslateCTM(ctx, size.width, 0);
        CGContextRotateCTM(ctx, M_PI_2);
    } else {
        CGContextTranslateCTM(ctx, 0, size.height);
        CGContextRotateCTM(ctx, -M_PI_2);
    }

The UI seems to be flipped (see how the shadow is on the top right instead of bottom right). I don't really understand why though. Will try look into it in more detail tonight, but maybe you already have guess or some solution up your sleeve.

screenshot 2014-05-26 18 34 55

(Top view: PSD, bottom view: Simulator)

}

[layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

- (CGSize)sizeForInterfaceOrientation {
CGSize size;
if ([self isInterfaceInPortrait]) {
size = [UIScreen mainScreen].bounds.size;
} else {
size = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
}

return size;
}

- (BOOL)isInterfaceInPortrait {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
return UIInterfaceOrientationIsPortrait(orientation);
}

@end