-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
TiUIImageViewProxy.m
191 lines (156 loc) Β· 3.69 KB
/
TiUIImageViewProxy.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
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
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#ifdef USE_TI_UIIMAGEVIEW
#import "TiUIImageViewProxy.h"
#import "TiUIImageView.h"
#import "OperationQueue.h"
#import "ASIHTTPRequest.h"
#import "TiApp.h"
#import "TiFile.h"
#import "TiBlob.h"
@implementation TiUIImageViewProxy
static NSArray* imageKeySequence;
#pragma mark Internal
-(NSArray *)keySequence
{
if (imageKeySequence == nil)
{
imageKeySequence = [[NSArray arrayWithObjects:@"width",@"height",nil] retain];
}
return imageKeySequence;
}
-(void)_configure
{
[self replaceValue:NUMBOOL(NO) forKey:@"animating" notification:NO];
[self replaceValue:NUMBOOL(NO) forKey:@"paused" notification:NO];
[self replaceValue:NUMBOOL(NO) forKey:@"reverse" notification:NO];
}
-(void)start:(id)args
{
ENSURE_UI_THREAD(start,args);
TiUIImageView *iv= (TiUIImageView*)[self view];
[iv start];
}
-(void)stop:(id)args
{
//Don't put this in UIThread, because it doesn't need to go in UIThread.
//Furthermore, by the time this is run, if this stop was called by a destroy
//Bad things(tm) happen.
[destroyLock lock];
if ([self viewAttached])
{
TiUIImageView *iv= (TiUIImageView*)[self view];
[iv stop];
}
[destroyLock unlock];
}
-(void)pause:(id)args
{
ENSURE_UI_THREAD(pause,args);
TiUIImageView *iv= (TiUIImageView*)[self view];
[iv pause];
}
-(void)viewWillDetach
{
[self stop:nil];
[super viewWillDetach];
}
-(void)windowWillClose
{
[self stop:nil];
[super windowWillClose];
}
-(void)_destroy
{
[self stop:nil];
[super _destroy];
}
- (void) dealloc
{
RELEASE_TO_NIL(urlRequest);
[super dealloc];
}
-(id)toBlob:(id)args
{
id imageValue = [self valueForKey:@"image"];
if (imageValue == nil)
{
imageValue = [self valueForKey:@"url"];
}
if ([imageValue isKindOfClass:[TiBlob class]])
{
//We already have it right here already!
return imageValue;
}
if ([imageValue isKindOfClass:[TiFile class]])
{
return [(TiFile *)imageValue toBlob:nil];
}
if (imageValue!=nil)
{
NSURL *url_ = [TiUtils toURL:imageValue proxy:self];
UIImage *image = [[ImageLoader sharedLoader] loadImmediateImage:url_];
if (image!=nil)
{
return [[[TiBlob alloc] initWithImage:image] autorelease];
}
// we're on the non-UI thread, we need to block to load
image = [[ImageLoader sharedLoader] loadRemote:url_];
return [[[TiBlob alloc] initWithImage:image] autorelease];
}
return nil;
}
USE_VIEW_FOR_AUTO_WIDTH
USE_VIEW_FOR_AUTO_HEIGHT
#pragma mark Handling ImageLoader
-(void)setImage:(id)newImage
{
if ([newImage isEqual:@""])
{
newImage = nil;
}
[self replaceValue:[self sanitizeURL:newImage] forKey:@"image" notification:YES];
}
-(void)startImageLoad:(NSURL *)url;
{
[self cancelPendingImageLoads]; //Just in case we have a crusty old urlRequest.
urlRequest = [[[ImageLoader sharedLoader] loadImage:url delegate:self userInfo:nil] retain];
}
-(void)cancelPendingImageLoads
{
// cancel a pending request if we have one pending
if (urlRequest!=nil)
{
[urlRequest cancel];
RELEASE_TO_NIL(urlRequest);
}
}
-(void)imageLoadSuccess:(ImageLoaderRequest*)request image:(UIImage*)image
{
if (request != urlRequest)
{
return;
}
if ([self viewAttached])
{
[(TiUIImageView *)[self view] imageLoadSuccess:request image:image];
}
RELEASE_TO_NIL(urlRequest);
}
-(void)imageLoadFailed:(ImageLoaderRequest*)request error:(NSError*)error
{
if (request == urlRequest)
{
NSLog(@"[ERROR] Failed to load image: %@, Error: %@",[request url], error);
RELEASE_TO_NIL(urlRequest);
}
}
-(void)imageLoadCancelled:(ImageLoaderRequest *)request
{
}
@end
#endif