-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGif.m
More file actions
136 lines (102 loc) · 4.46 KB
/
Gif.m
File metadata and controls
136 lines (102 loc) · 4.46 KB
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
//
// Gif.m
// Giffy
//
// Created by Eugene on 2016-09-13.
// Copyright © 2016 Eugene. All rights reserved.
//
#import "Gif.h"
@import UIKit;
// NSCoding keys
static NSString * const identifierKey = @"identifier";
static NSString * const tagsKey = @"tags";
static NSString * const webURLKey = @"webURL";
static NSString * const imageDownloadedKey = @"imageDownloaded";
static NSString * const ratingKey = @"rating";
// NSNotification key
NSString * const gifDataDidChangeNotification = @"gifDataDidChangeNotification";
typedef NS_ENUM(NSInteger, imageScaleOption) {
imageScaleOptionFit,
imageScaleOptionFill
};
@interface Gif ()
@property (strong, nonatomic) NSString *databaseFilePath;
@end
@implementation Gif
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (!self) {
return nil;
}
self.identifier = [aDecoder decodeObjectForKey:identifierKey];
self.tags = [aDecoder decodeObjectForKey:tagsKey];
self.webURL = [aDecoder decodeObjectForKey:webURLKey];
self.imageDownloaded = [aDecoder decodeBoolForKey:imageDownloadedKey];
self.rating = [aDecoder decodeIntegerForKey:ratingKey];
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.identifier forKey:identifierKey];
[aCoder encodeObject:self.tags forKey:tagsKey];
[aCoder encodeObject:self.webURL forKey:webURLKey];
[aCoder encodeBool:self.imageDownloaded forKey:imageDownloadedKey];
[aCoder encodeInteger:self.rating forKey:ratingKey];
}
- (void)saveData {
NSString *documentsPath = [[self databaseFilePath] stringByAppendingPathComponent:@"data.plist"];
[NSKeyedArchiver archiveRootObject:self toFile:documentsPath];
[[NSNotificationCenter defaultCenter] postNotificationName:gifDataDidChangeNotification object:self];
}
- (void)saveImage:(NSURL *)tempLocation {
NSData *gifData = [NSData dataWithContentsOfURL:tempLocation];
UIImage *image = [UIImage imageWithData:gifData];
NSString *documentsPath = [[self databaseFilePath] stringByAppendingPathComponent:@"image.gif"];
[[NSFileManager defaultManager] createFileAtPath:documentsPath contents:gifData attributes:nil];
UIImage *thumbnail = [Gif imageWithImage:image scaledToSize:CGSizeMake(60, 60) scaleOption:imageScaleOptionFill];
NSData *thumbnailData = UIImagePNGRepresentation(thumbnail);
documentsPath = [[self databaseFilePath] stringByAppendingPathComponent:@"thumbnail.png"];
[[NSFileManager defaultManager] createFileAtPath:documentsPath contents:thumbnailData attributes:nil];
self.imageDownloaded = YES;
[self saveData];
}
- (NSString *)imagePath {
return [self.databaseFilePath stringByAppendingPathComponent:@"image.gif"];
}
- (NSString *)thumbnailPath {
return [self.databaseFilePath stringByAppendingPathComponent:@"thumbnail.png"];
}
- (NSString *)databaseFilePath {
if (!_databaseFilePath) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
documentsPath = [NSString pathWithComponents:@[documentsPath, @"Database", self.identifier]];
documentsPath = [documentsPath stringByAppendingPathExtension:@"gifData"];
if (![fileManager fileExistsAtPath:documentsPath]) {
[fileManager createDirectoryAtPath:documentsPath withIntermediateDirectories:YES attributes:nil error:nil];
}
_databaseFilePath = documentsPath;
}
return _databaseFilePath;
}
#pragma mark - Image Helper
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)size scaleOption:(imageScaleOption)option {
CGFloat scale;
switch (option) {
case imageScaleOptionFill:
scale = MAX(size.width/image.size.width, size.height/image.size.height);
break;
case imageScaleOptionFit:
default:
scale = MIN(size.width/image.size.width, size.height/image.size.height);
break;
}
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;
}
@end