-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyPhoto.m
115 lines (81 loc) · 2.62 KB
/
MyPhoto.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// MyPhoto.m
// Gallery
//
// Created by David Kapp on 2/27/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "MyPhoto.h"
#import <Quartz/Quartz.h>
@interface MyPhoto ()
@property (retain) NSImage *thumbnail;
- (void) generateUniqueID;
@end
@implementation MyPhoto
// Core Data properties are generated at run-time, not compile-time, so use @dynamic instead of @synthesize
@dynamic filePath;
@dynamic uniqueID;
@dynamic orderIndex;
@dynamic album;
// normal properties
@synthesize thumbnail;
+ (id) photoInDefaultContext {
NSManagedObjectContext *context = [[NSApp delegate] managedObjectContext];
MyPhoto *newItem = [NSEntityDescription insertNewObjectForEntityForName:@"Photo"
inManagedObjectContext:context];
newItem.filePath = nil;
return newItem;
}
- (NSImage*) largeThumbnail {
// the large thumbnail is used by the list view
if( self.thumbnail ) return self.thumbnail;
NSSize size = NSMakeSize(250, 250);
CFStringRef path = (CFStringRef)self.filePath;
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, NO);
// use QuickLook to generate a thumbnail of the image
CGImageRef thumb = QLThumbnailImageCreate(NULL, url, size, nil);
NSImage *image = [[NSImage alloc] initWithCGImage:thumb size:size];
self.thumbnail = image;
CFRelease(url);
CFRelease(thumb);
[image release];
return image;
}
#pragma mark -
#pragma mark Core Data Methods
- (void) awakeFromInsert {
// called when the object is first created
[self generateUniqueID];
}
#pragma mark -
#pragma mark IKIBrowserItem protocol methods
- (NSString*) imageTitle {
NSString *fullFileName = self.filePath.lastPathComponent;
return [fullFileName stringByDeletingPathExtension];
}
- (NSString*) imageUID {
// return uniqueID if it exists - otherwise make then return
NSString *uniqueID = self.uniqueID;
if( uniqueID ) return uniqueID;
[self generateUniqueID];
return self.uniqueID;
}
- (NSString*) imageRepresentationType {
return IKImageBrowserPathRepresentationType;
}
- (id) imageRepresentation {
return self.filePath;
}
#pragma mark -
#pragma mark private methods
- (void) generateUniqueID {
NSString *uniqueID = self.uniqueID;
if( uniqueID != nil ) return;
self.uniqueID = [[NSProcessInfo processInfo] globallyUniqueString];
}
- (void) dealloc {
// core data properties are automatically managed, only release synthesized properties
self.thumbnail = nil;
[super dealloc];
}
@end