Skip to content

Instantly share code, notes, and snippets.

@millenomi
Created August 11, 2009 09:47
Show Gist options
  • Save millenomi/165725 to your computer and use it in GitHub Desktop.
Save millenomi/165725 to your computer and use it in GitHub Desktop.
//
// L0Photoshoot.m
// Photoshoot
//
// Created by ∞ on 11/08/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "L0Photoshoot.h"
#import <Quartz/Quartz.h>
@interface L0Photoshoot ()
- (NSNumber*) PIDOfSimulator;
- (NSArray*) iPhoneSimulatorWindows;
@end
static BOOL L0IsLandscape(NSSize s) {
return s.height < s.width;
}
const NSRect L0iPhoneScreenRect_PortraitWithStatusBar = { { 47, 139 }, { 320, 480 } };
const NSRect L0iPhoneScreenRect_PortraitWithoutStatusBar = { { 47, 159 }, { 320, 460 } };
@implementation L0Photoshoot
- (IBAction) takePhotoOfiPhoneSimulator:(id) sender;
{
static int index = 0;
NSArray* a = [self iPhoneSimulatorWindows];
if (!a || [a count] == 0) { NSBeep(); return; }
NSDictionary* d = [a objectAtIndex:0];
const void* thisWindow = (const void*) [[d objectForKey:(id) kCGWindowNumber] longValue];
CFArrayRef thisWindowAsArray = CFMakeCollectable(CFArrayCreate(kCFAllocatorDefault, &thisWindow, 1, NULL));
CGImageRef screenshot = (CGImageRef) CFMakeCollectable(CGWindowListCreateImageFromArray(CGRectNull, thisWindowAsArray, kCGWindowImageDefault));
NSBitmapImageRep* shotRep = [[NSBitmapImageRep alloc] initWithCGImage:screenshot];
NSImage* shot = [[NSImage alloc] initWithSize:[shotRep size]];
[shot addRepresentation:shotRep];
[shot setFlipped:YES];
// in flipped coords, 47<>139 extent: 320<>480.
NSImage* i = [[NSImage alloc] initWithSize:L0iPhoneScreenRect_PortraitWithoutStatusBar.size];
[i setFlipped:YES];
[i lockFocus];
//[shot drawInRect:NSMakeRect(0, 0, 320, 480) fromRect:NSMakeRect(47, 151, 320, 480) operation:NSCompositeSourceOver fraction:1.0];
NSRect r; r.origin = NSZeroPoint; r.size = [i size];
[shot drawInRect:r fromRect:L0iPhoneScreenRect_PortraitWithoutStatusBar operation:NSCompositeSourceOver fraction:1.0];
[i unlockFocus];
index++;
[[i TIFFRepresentation] writeToFile:[NSString stringWithFormat:@"/Users/millenomi/Desktop/Shot %d.tiff", index] atomically:YES];
}
- (NSArray*) iPhoneSimulatorWindows;
{
NSNumber* pid = [self PIDOfSimulator];
if (!pid) return nil;
NSArray* a = NSMakeCollectable(CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID));
NSMutableArray* result = [NSMutableArray array];
for (NSDictionary* window in a) {
if ([[window objectForKey:(id) kCGWindowOwnerPID] isEqual:pid]) {
[result addObject:window];
}
}
return result;
}
- (NSNumber*) PIDOfSimulator;
{
for (NSDictionary* app in [[NSWorkspace sharedWorkspace] launchedApplications]) {
if ([[app objectForKey:@"NSApplicationBundleIdentifier"] isEqual:@"com.apple.iphonesimulator"] && [app objectForKey:@"NSApplicationProcessIdentifier"]) {
return [app objectForKey:@"NSApplicationProcessIdentifier"];
}
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment