summaryrefslogtreecommitdiffstats
path: root/Classes/HJManagedImageV.m
blob: eb83f7dad703915f7104102409c8fd5662c19311 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
//  HJManagedImageV.m
//  hjlib
//
//  Copyright Hunter and Johnson 2009, 2010, 2011
//  HJCache may be used freely in any iOS or Mac application free or commercial.
//  May be redistributed as source code only if all the original files are included.
//  See http://www.markj.net/hjcache-iphone-image-cache/

#import "HJManagedImageV.h"

@interface UIImage (Extras)

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize;

@end

@implementation UIImage (Extras)

#pragma mark -
#pragma mark Scale and crop image

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
	UIImage *sourceImage = self;
	UIImage *newImage = nil;        
	CGSize imageSize = sourceImage.size;
	CGFloat width = imageSize.width;
	CGFloat height = imageSize.height;
	CGFloat targetWidth = targetSize.width;
	CGFloat targetHeight = targetSize.height;
	CGFloat scaleFactor = 0.0;
	CGFloat scaledWidth = targetWidth;
	CGFloat scaledHeight = targetHeight;
	CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
	
	if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
	{
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
		
        if (widthFactor > heightFactor) 
			scaleFactor = widthFactor; // scale to fit height
        else
			scaleFactor = heightFactor; // scale to fit width
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;
		
        // center the image
        if (widthFactor > heightFactor)
		{
			thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
		}
        else 
			if (widthFactor < heightFactor)
			{
				thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
			}
	}       
	
	UIGraphicsBeginImageContext(targetSize); // this will crop
	
	CGRect thumbnailRect = CGRectZero;
	thumbnailRect.origin = thumbnailPoint;
	thumbnailRect.size.width  = scaledWidth;
	thumbnailRect.size.height = scaledHeight;
	
	[sourceImage drawInRect:thumbnailRect];
	
	newImage = UIGraphicsGetImageFromCurrentImageContext();
	if(newImage == nil) 
        NSLog(@"could not scale image");
	
	//pop the context to get back to the default
	UIGraphicsEndImageContext();
	return newImage;
}

@end

@implementation HJManagedImageV


@synthesize oid;
@synthesize url;
@synthesize moHandler;
@synthesize squareCropped;
@synthesize callbackOnSetImage;
@synthesize callbackOnCancel;
@synthesize imageView;
@synthesize modification;
@synthesize loadingWheel;
@synthesize index;


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
		isCancelled=NO;
		modification=0;
		url=nil;
		onImageTap = nil;
		squareCropped = false;
		index = -1;
		self.userInteractionEnabled = NO; //because want to treat it like a UIImageView. Just turn this back on if you want to catch taps.
    }
    return self;
}

- (void)dealloc {
	[self clear];
	self.callbackOnCancel=nil;
	self.callbackOnSetImage=nil;
	self.loadingWheel=nil;
    [super dealloc];
	//NSLog(@"ManagedImage dealloc");
}


-(void) clear {
	[self.moHandler removeUser:self];
	self.moHandler=nil;
	[imageView removeFromSuperview];
	self.image = nil;
	self.imageView.image=nil;
	self.imageView=nil;
	self.oid=nil;
	self.url=nil;
}

/*
-(void) clear {
	self.url = nil;
	self.callbackOnSetImage = nil;
	//int rc1 = [image retainCount];
	[self.imageView removeFromSuperview];
	self.imageView = nil;
	//int rc2 = [image retainCount];
	[image release]; image=nil; //do this instead of self.image=nil because setImage has more code
	self.loadingWheel = nil;
}
*/


-(void) changeManagedObjStateFromLoadedToReady {
	//NSLog(@"managedStateReady %@",managedState);
	if (moHandler.moData) {
		moHandler.managedObj=[UIImage imageWithData:moHandler.moData];
	} else if (moHandler.moReadyDataFilename) {
		moHandler.managedObj=[UIImage imageWithContentsOfFile:moHandler.moReadyDataFilename];
	} else {
		//error? 
		NSLog(@"HJManagedImageV error in changeManagedObjStateFromLoadedToReady ?");
	}
}

-(void) managedObjFailed {
	NSLog(@"moHandlerFailed %@",moHandler);
	[image release];
	image = nil;
}

-(void) managedObjReady {
	//NSLog(@"moHandlerReady %@",moHandler);
	[self setImage:moHandler.managedObj];
}


-(UIImage*) image {
	return image; 
}

-(void) markCancelled {
	isCancelled = YES;
	[callbackOnCancel managedImageCancelled:self];
}

-(UIImage*) modifyImage:(UIImage*)theImage modification:(int)mod {
	return theImage;
}


-(void) setImage:(UIImage*)theImage modification:(int)mod {
	if (mod==modification) {
		[self setImage:theImage];
	} else {
		UIImage* modified = [self modifyImage:theImage modification:(int)mod];
		[self setImage:modified];
	}
}


-(void) setImage:(UIImage*)theImage {
	if (theImage==image) {
		//when the same image is on the screen multiple times, an image that is alredy set might be set again with the same image.
		return; 
	}
	[theImage retain];
	[image release];
	image = theImage;
	
	[imageView removeFromSuperview];
	
	if (squareCropped) 		
		self.imageView = [[[UIImageView alloc] initWithImage:[theImage imageByScalingAndCroppingForSize:CGSizeMake(100,100)]] autorelease];
	else
		self.imageView = [[[UIImageView alloc] initWithImage:theImage] autorelease];
	
	imageView.contentMode = UIViewContentModeScaleAspectFit;
	imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
	[self addSubview:imageView];
	imageView.frame = self.bounds;
	[imageView setNeedsLayout];
	[self setNeedsLayout];
	//NSLog(@"setImageCallback from %@ to %@",self,callbackOnSetImage);
	[loadingWheel stopAnimating];
	[loadingWheel removeFromSuperview];
	self.loadingWheel = nil;
	self.hidden=NO;
	if (image!=nil) {
		[callbackOnSetImage managedImageSet:self];
	}
}

-(void) showLoadingWheel {
	[loadingWheel removeFromSuperview];
	self.loadingWheel = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
	loadingWheel.center = self.center;
	loadingWheel.hidesWhenStopped=YES;
	[self addSubview:loadingWheel];
	[loadingWheel startAnimating];
}

-(void) setCallbackOnImageTap:(id)obj method:(SEL)m {
	NSInvocation* invo = [NSInvocation invocationWithMethodSignature:[obj methodSignatureForSelector:m]]; 
	[invo setTarget:obj];
	[invo setSelector:m];
	[invo setArgument:&self atIndex:2];
	[invo retain];
	[onImageTap release];
	onImageTap = invo;
	self.userInteractionEnabled=YES; //because it's NO in the initializer, but if we want to get a callback on tap, 
									 //then need to get touch events.
}

-(void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
	if (onImageTap) {
		[onImageTap invoke];
	}
    else {
        [super touchesEnded:touches withEvent:event];
    }
}

@end