-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImleeModel.m
146 lines (125 loc) · 5.39 KB
/
ImleeModel.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
146
//
// ImleeModel.m
// Imlee
//
// Created by Devarshi Kulshreshtha on 6/21/12.
// Copyright 2012 DaemonConstruction. All rights reserved.
//
#import "ImleeModel.h"
@implementation ImleeModel
#pragma mark synthesises
@synthesize imageTypes = _imageTypes;
@synthesize currentImage = _currentImage;
@synthesize currentImagePath = _currentImagePath;
@synthesize currentImageType = _currentImageType;
@synthesize imageTypeSelectedDict = _imageTypeSelectedDict;
@synthesize isSelectedImageTypeSameAsCurrentImageType = _isSelectedImageTypeSameAsCurrentImageType;
#pragma mark initialization
- (id)init
{
if (self = [super init]) {
// code to initialize certain properties
self.isSelectedImageTypeSameAsCurrentImageType = YES;
NSDictionary *pngTypeDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"png",@"imageTypeName",[NSNumber numberWithInt:NSPNGFileType], @"imageTypeValue",nil];
NSDictionary *gifTypeDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"gif",@"imageTypeName",[NSNumber numberWithInt:NSGIFFileType], @"imageTypeValue",nil];
NSDictionary *bmpTypeDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"bmp",@"imageTypeName",[NSNumber numberWithInt:NSBMPFileType], @"imageTypeValue",nil];
NSDictionary *jpgTypeDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"jpg",@"imageTypeName",[NSNumber numberWithInt:NSJPEGFileType], @"imageTypeValue",nil];
NSDictionary *jpegTypeDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"jpeg",@"imageTypeName",[NSNumber numberWithInt:NSJPEGFileType], @"imageTypeValue",nil];
NSDictionary *tiffTypeDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"tiff",@"imageTypeName",[NSNumber numberWithInt:NSTIFFFileType], @"imageTypeValue",nil];
NSArray *tempArray = [[NSArray alloc] initWithObjects:pngTypeDict,gifTypeDict,bmpTypeDict,jpgTypeDict,jpegTypeDict,tiffTypeDict,nil];
self.imageTypes = tempArray;
[tempArray release];
[pngTypeDict release];
[gifTypeDict release];
[bmpTypeDict release];
[jpgTypeDict release];
[jpegTypeDict release];
[tiffTypeDict release];
// adding observer
[self addObserver:self forKeyPath:@"currentImagePath" options:NSKeyValueObservingOptionNew context:NULL];
[self addObserver:self forKeyPath:@"currentImageType" options:NSKeyValueObservingOptionNew context:NULL];
[self addObserver:self forKeyPath:@"imageTypeSelectedDict" options:NSKeyValueObservingOptionNew context:NULL];
}
return self;
}
#pragma mark observing keys
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"currentImagePath"])
{
if ([[change objectForKey:NSKeyValueChangeNewKey] isEqual:[NSNull null]]) {
self.currentImageType = nil;
}
else {
self.currentImageType = [[change objectForKey:NSKeyValueChangeNewKey] pathExtension];
}
}
else if ([keyPath isEqualToString:@"currentImageType"])
{
// current image type is nil so image type selected should also be nil
if (self.currentImageType == nil)
{
self.imageTypeSelectedDict = nil;
}
// current image type is changed so selected image type should also be changed
else
{
//TODO: try it with predicate with block
NSPredicate *imageTypeFilterPredicate = [NSPredicate predicateWithFormat:@"imageTypeName like %@",self.currentImageType];
self.imageTypeSelectedDict = [[self.imageTypes filteredArrayUsingPredicate:imageTypeFilterPredicate] objectAtIndex:0];
}
}
// condition check to enable-disable save button
else if ([keyPath isEqualToString:@"imageTypeSelectedDict"])
{
// current image path is nil so save button should be disabled
if (self.imageTypeSelectedDict == nil)
{
self.isSelectedImageTypeSameAsCurrentImageType = YES;
}
// pop up button value is same as dropped image type so save button should be disabled
else if ([[[change objectForKey:NSKeyValueChangeNewKey] objectForKey:@"imageTypeName"] isEqualToString:[self.currentImagePath pathExtension]])
{
self.isSelectedImageTypeSameAsCurrentImageType = YES;
}
// pop up button value is not same as dropped image type so save button should be enabled
else
{
self.isSelectedImageTypeSameAsCurrentImageType = NO;
}
}
}
#pragma mark actions associated with UI elements
- (void)save
{
NSBitmapImageRep *bits = [[self.currentImage representations] objectAtIndex:0];
NSString *imageNewExtension = [self.imageTypeSelectedDict objectForKey:@"imageTypeName"];
NSData *data = [bits representationUsingType:[[self.imageTypeSelectedDict objectForKey:@"imageTypeValue"] intValue] properties:nil];
//changing name of file
NSString *imagePathWithoutExtension = [self.currentImagePath stringByDeletingPathExtension];
NSString *imagePathWithNewExtension = [[NSString alloc] initWithFormat:@"%@.%@",imagePathWithoutExtension,imageNewExtension];
[data writeToFile:imagePathWithNewExtension atomically:NO];
// re-assigning values to properties
self.currentImagePath = imagePathWithNewExtension;
[imagePathWithNewExtension release];
}
- (void)clearAll
{
self.currentImage = nil; // showing no image on image view
self.currentImagePath = nil; // showing no image path
}
- (void)openImage
{
[[NSWorkspace sharedWorkspace] openFile:self.currentImagePath];
}
#pragma mark releasing used resources
- (void)dealloc
{
[_imageTypes release];
[_currentImage release];
[_currentImagePath release];
[_currentImageType release];
[_imageTypeSelectedDict release];
[super dealloc];
}
@end