-
Notifications
You must be signed in to change notification settings - Fork 2
/
ALAssetsLibraryPhotoSource.m
56 lines (44 loc) · 1.25 KB
/
ALAssetsLibraryPhotoSource.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
#import "ALAssetsLibraryPhotoSource.h"
#import "ALAssetsLibraryPhoto.h"
@implementation ALAssetsLibraryPhotoSource
@synthesize cache;
- (id)initWithAssets:(NSArray *)assets
{
self = [super init];
self.cache = [NSCache new];
self.cache.delegate = self;
self.cache.countLimit = 4;
NSMutableArray *photos = [NSMutableArray new];
for (ALAsset *asset in assets) {
ALAssetsLibraryPhoto *photo = [ALAssetsLibraryPhoto new];
photo.asset = asset;
[photos addObject:photo];
}
_photos = photos;
return self;
}
- (id<EGOPhoto>)photoAtIndex:(NSInteger)index
{
ALAssetsLibraryPhoto *photo = [self.photos objectAtIndex:index];
if (photo.image == nil) {
ALAssetRepresentation *representation = photo.asset.defaultRepresentation;
CGImageRef imageRef = representation.fullScreenImage;
photo.image = [UIImage imageWithCGImage:imageRef scale:representation.scale orientation:(UIImageOrientation) [photo.asset valueForProperty:@"ALAssetPropertyOrientation"]];
[cache setObject:photo forKey:[NSNumber numberWithInt:index]];
}
return photo;
}
- (NSArray *)photos
{
return _photos;
}
- (NSInteger)numberOfPhotos
{
return [self.photos count];
}
- (void)cache:(NSCache *)cache willEvictObject:(id)obj
{
ALAssetsLibraryPhoto *photo = obj;
photo.image = nil;
}
@end