-
Notifications
You must be signed in to change notification settings - Fork 4
/
LHiPhotoLibrary.m
110 lines (92 loc) · 2.58 KB
/
LHiPhotoLibrary.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
//
// LHiPhotoLibrary.m
// LastHistory
//
// Created by Frederik Seiffert on 08.11.09.
// Copyright 2009 Frederik Seiffert. All rights reserved.
//
#import "LHiPhotoLibrary.h"
@implementation LHiPhotoLibrary
@synthesize libraryURL=_libraryURL;
@synthesize rolls=_rolls;
+ (LHiPhotoLibrary *)defaultLibrary
{
static id defaultLibrary = nil;
if (!defaultLibrary) {
[[NSUserDefaults standardUserDefaults] synchronize];
NSArray *dbs = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"com.apple.iApps"] objectForKey:@"iPhotoRecentDatabases"];
if ([dbs count] > 0) {
NSURL *url = [NSURL URLWithString:[dbs objectAtIndex:0]];
if ([url isFileURL])
defaultLibrary = [[self alloc] initWithURL:url];
}
}
return defaultLibrary;
}
- (id)initWithURL:(NSURL *)libraryURL
{
self = [super init];
if (self != nil) {
_libraryURL = libraryURL;
#if USE_IMAGE_CACHE
_imageCache = [NSMapTable mapTableWithStrongToWeakObjects];
#endif
[self loadLibrary];
}
return self;
}
- (LHiPhotoPhoto *)imageForKey:(NSString *)key inRoll:(LHiPhotoRoll *)roll
{
#if USE_IMAGE_CACHE
LHiPhotoPhoto *result = [_imageCache objectForKey:key];
#else
LHiPhotoPhoto *result = nil;
#endif
if (!result)
{
NSDictionary *imageDict = [_imageDictsByKey objectForKey:key];
if (imageDict) {
result = [[LHiPhotoPhoto alloc] initWithDictionary:imageDict inRoll:roll];
#if USE_IMAGE_CACHE
[_imageCache setObject:result forKey:key];
#endif
}
}
return result;
}
- (BOOL)loadLibrary
{
if (_rolls && _imageDictsByKey)
return YES;
NSURL *libraryURL = self.libraryURL;
if (!libraryURL)
return NO;
NSLog(@"Reading iPhoto library: %@", libraryURL);
NSDictionary *library = [NSDictionary dictionaryWithContentsOfURL:libraryURL];
if (!library) {
NSLog(@"Error: unable to read iPhoto library from '%@'.", libraryURL);
return NO;
}
NSArray *rollsDicts = [library objectForKey:@"List of Rolls"];
if (!rollsDicts) {
NSLog(@"Error: No rolls found in iPhoto library.");
} else {
NSMutableArray *rolls = [NSMutableArray arrayWithCapacity:rollsDicts.count];
for (NSDictionary *rollDict in rollsDicts)
{
LHiPhotoRoll *roll = [[LHiPhotoRoll alloc] initWithDictionary:rollDict forLibrary:self];
[rolls addObject:roll];
}
NSLog(@"Read %d rolls from iPhoto.", rolls.count);
_rolls = [rolls copy];
}
NSDictionary *images = [library objectForKey:@"Master Image List"];
if (!images) {
NSLog(@"Error: No images found in iPhoto library.");
} else {
NSLog(@"Read %d images from iPhoto.", images.count);
_imageDictsByKey = [images copy];
}
return _rolls && _imageDictsByKey;
}
@end