-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhotoStorage.m
145 lines (123 loc) · 4.62 KB
/
PhotoStorage.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// PhotoStorage.m
// Calculator
//
// Created by Corey Allen Pett on 11/10/15.
// Copyright © 2015 Corey Allen Pett. All rights reserved.
//
#import "PhotoStorage.h"
@implementation PhotoStorage
//Fetch the users saved albums
-(void)fetchAlbums
{
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Album"];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];
self.userAlbums = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
}
//Fetch the users saved photos from selected album
-(void)fetchPhotos
{
self.currentPhoto = -1;
self.scaledImageArray = nil;
self.fullImageArray = nil;
for (Photo *photo in self.selectedAlbum.photos){
UIImage *image = [UIImage imageWithData:photo.thumbnail];
[self.fullImageArray addObject:photo.image];
[self.scaledImageArray addObject:image];
self.currentPhoto++;
}
}
//Delete current displayed photo
-(void)deletePhoto
{
NSManagedObjectContext *context = [self managedObjectContext];
[self.selectedAlbum removeObjectFromPhotosAtIndex:self.currentPhoto];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
}
//Delete chosen album
-(void)deleteAlbum:(long)index
{
NSManagedObjectContext *context = [self managedObjectContext];
[context deleteObject:[self.userAlbums objectAtIndex:index]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
[self.userAlbums removeObjectAtIndex:index];
}
//Add selected photo to selected album
-(void)addPhotoToAlbum:(UIImage *)image
{
NSManagedObjectContext *context = [self managedObjectContext];
UIImage *scaleImage = [self imageWithImage:image scaledToFillSize:CGSizeMake(64,64)];
NSData *fullImage = UIImageJPEGRepresentation(image, 1.0);
NSData *scaledImage = UIImageJPEGRepresentation(scaleImage, 1.0);
Photo *addPhoto = [NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext:context];
addPhoto.date = [NSDate date];
addPhoto.image = fullImage;
addPhoto.thumbnail = scaledImage;
[self.fullImageArray addObject:fullImage];
[self.scaledImageArray addObject:scaleImage];
[self.selectedAlbum addPhotosObject:addPhoto];
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
//Create a new album and save it
-(void)createNewAlbum:(NSString *)title
{
NSManagedObjectContext *context = [self managedObjectContext];
Album *album = [NSEntityDescription insertNewObjectForEntityForName:@"Album" inManagedObjectContext:context];
album.title = title;
album.date = [NSDate date];
[self.userAlbums addObject:album];
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
//To scale down the images for UICollectionView to display #performanceissues
-(UIImage *)imageWithImage:(UIImage *)image scaledToFillSize:(CGSize)size
{
CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height);
CGFloat width = image.size.width * scale;
CGFloat height = image.size.height * scale;
CGRect imageRect = CGRectMake((size.width - width)/2.0f,
(size.height - height)/2.0f,
width,
height);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
[image drawInRect:imageRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
//Set up NSManagedObject for fetching
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
-(NSMutableArray *)fullImageArray
{
if(!_fullImageArray) _fullImageArray = [[NSMutableArray alloc] init];
return _fullImageArray;
}
-(NSMutableArray *) scaledImageArray
{
if(!_scaledImageArray) _scaledImageArray = [[NSMutableArray alloc] init];
return _scaledImageArray;
}
@end