Skip to content

Commit

Permalink
Merge pull request #1492 from sptramer/timob-7681
Browse files Browse the repository at this point in the history
[TIMOB-7681] Preserve remote hires when caching + local file load fixes
  • Loading branch information
Max Stepanov committed Feb 24, 2012
2 parents bfbac89 + 4d46860 commit db2ba90
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 76 deletions.
32 changes: 19 additions & 13 deletions iphone/Classes/ImageLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ -(ImageCacheEntry*)initWithURL:(NSURL *)url
if (self = [super init]) {
remoteURL = [url retain];
local = NO;

if ([remoteURL isFileURL]) {
localPath = [[remoteURL path] retain];
local = YES;
Expand All @@ -222,11 +223,16 @@ -(void)serialize:(NSData*)imageData
{
if (!local && imageData != nil) {
NSFileManager* fm = [NSFileManager defaultManager];
if ([fm isDeletableFileAtPath:localPath]) {
[fm removeItemAtPath:localPath error:nil];
NSString* path = localPath;
if (hires && [TiUtils isRetinaDisplay]) { // Save as @2x w/retina
path = [NSString stringWithFormat:@"%@@2x.%@", [localPath stringByDeletingPathExtension], [localPath pathExtension]];
}

if ([fm isDeletableFileAtPath:path]) {
[fm removeItemAtPath:path error:nil];
}
if (![fm createFileAtPath:localPath contents:imageData attributes:nil]) {
NSLog(@"[WARN] Unknown error serializing image %@ to path %@", remoteURL, localPath);
if (![fm createFileAtPath:path contents:imageData attributes:nil]) {
NSLog(@"[WARN] Unknown error serializing image %@ to path %@", remoteURL, path);
}
}
}
Expand Down Expand Up @@ -378,7 +384,7 @@ +(ImageLoader*)sharedLoader
return sharedLoader;
}

-(ImageCacheEntry *)setImage:(id)image forKey:(NSURL *)url
-(ImageCacheEntry *)setImage:(id)image forKey:(NSURL *)url hires:(BOOL)hires;
{
NSString *urlString = [url absoluteString];
if (image==nil)
Expand All @@ -396,6 +402,7 @@ -(ImageCacheEntry *)setImage:(id)image forKey:(NSURL *)url
#endif
}
ImageCacheEntry * newEntry = [[[ImageCacheEntry alloc] initWithURL:url] autorelease];
[newEntry setHires:hires];

if ([image isKindOfClass:[UIImage class]]) {
[newEntry setFullImage:image];
Expand Down Expand Up @@ -469,7 +476,7 @@ -(ImageCacheEntry *)entryForKey:(NSURL *)url
resultImage = [UIImage imageWithCGImage:[resultImage CGImage] scale:2.0 orientation:[resultImage imageOrientation]];
}
}
result = [self setImage:resultImage forKey:url];
result = [self setImage:resultImage forKey:url hires:NO];
}
else // Check and see if we cached a file to disk
{
Expand All @@ -479,22 +486,22 @@ -(ImageCacheEntry *)entryForKey:(NSURL *)url
NSLog(@"[CACHE DEBUG] Retrieving local image [prefetch]: %@", diskCache);
#endif
UIImage* resultImage = [UIImage imageWithContentsOfFile:diskCache];
result = [self setImage:resultImage forKey:url];
result = [self setImage:resultImage forKey:url hires:NO];
}
}
}

return result;
}

-(id)cache:(id)image forURL:(NSURL*)url size:(CGSize)imageSize
-(id)cache:(id)image forURL:(NSURL*)url size:(CGSize)imageSize hires:(BOOL)hires
{
return [[self setImage:image forKey:url] imageForSize:imageSize];
return [[self setImage:image forKey:url hires:hires] imageForSize:imageSize];
}

-(id)cache:(id)image forURL:(NSURL*)url
{
return [self cache:image forURL:url size:CGSizeZero];
return [self cache:image forURL:url size:CGSizeZero hires:NO];
}

-(id)loadRemote:(NSURL*)url
Expand All @@ -516,7 +523,7 @@ -(id)loadRemote:(NSURL*)url
{
NSData *data = [req responseData];
UIImage *resultImage = [UIImage imageWithData:data];
ImageCacheEntry *result = [self setImage:resultImage forKey:url];
ImageCacheEntry *result = [self setImage:resultImage forKey:url hires:NO];
[result setData:data];
return [result imageForSize:CGSizeZero];
}
Expand Down Expand Up @@ -740,9 +747,8 @@ -(void)queueRequestDidFinish:(ASIHTTPRequest*)request
{
BOOL hires = [TiUtils boolValue:[[req userInfo] valueForKey:@"hires"] def:NO];

[self cache:data forURL:[req url]];
[self cache:data forURL:[req url] size:CGSizeZero hires:hires];
ImageCacheEntry *entry = [self entryForKey:[req url]];
[entry setHires:hires];

image = [entry fullImage];
}
Expand Down
120 changes: 57 additions & 63 deletions iphone/Classes/TiUIImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,30 @@ -(void)cancelPendingImageLoads
placeholderLoading = NO;
}

-(void)loadDefaultImage:(CGSize)imageSize
{
// use a placeholder image - which the dev can specify with the
// defaultImage property or we'll provide the Titanium stock one
// if not specified
NSURL *defURL = [TiUtils toURL:[self.proxy valueForKey:@"defaultImage"] proxy:self.proxy];

if ((defURL == nil) && ![TiUtils boolValue:[self.proxy valueForKey:@"preventDefaultImage"] def:NO])
{ //This is a special case, because it IS built into the bundle despite being in the simulator.
NSString * filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"modules/ui/images/photoDefault.png"];
defURL = [NSURL fileURLWithPath:filePath];
}

if (defURL!=nil)
{
UIImage *poster = [[ImageLoader sharedLoader] loadImmediateImage:defURL withSize:imageSize];

// TODO: Use the full image size here? Auto width/height is going to be changed once the image is loaded.
autoWidth = poster.size.width;
autoHeight = poster.size.height;
[self imageView].image = poster;
}
}

-(void)loadUrl:(id)img
{
[self cancelPendingImageLoads];
Expand All @@ -427,42 +451,43 @@ -(void)loadUrl:(id)img
[self removeAllImagesFromContainer];

NSURL *url_ = [TiUtils toURL:[img absoluteString] proxy:self.proxy];
// NOTE: Loading from URL means we can't pre-determine any % value.
// NOTE: Loading from URL means we can't pre-determine any % value.
CGSize imageSize = CGSizeMake(TiDimensionCalculateValue(width, 0.0),
TiDimensionCalculateValue(height,0.0));

if ([TiUtils boolValue:[[self proxy] valueForKey:@"hires"]])
{
imageSize.width *= 2;
imageSize.height *= 2;
}


// Skip the imageloader completely if this is obviously a file we can load off the fileystem.
// why were we ever doing that in the first place...?
if ([url_ isFileURL]) {
UIImage* image = [UIImage imageWithContentsOfFile:[url_ path]];
if (image != nil) {
CGSize fullSize = [image size];
autoWidth = fullSize.width;
autoHeight = fullSize.height;
[self imageView].image = image;
}
else {
[self loadDefaultImage:imageSize];
}
return;
}


UIImage *image = [[ImageLoader sharedLoader] loadImmediateImage:url_ withSize:imageSize];
if (image==nil)
{
// use a placeholder image - which the dev can specify with the
// defaultImage property or we'll provide the Titanium stock one
// if not specified
NSURL *defURL = [TiUtils toURL:[self.proxy valueForKey:@"defaultImage"] proxy:self.proxy];

if ((defURL == nil) && ![TiUtils boolValue:[self.proxy valueForKey:@"preventDefaultImage"] def:NO])
{ //This is a special case, because it IS built into the bundle despite being in the simulator.
NSString * filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"modules/ui/images/photoDefault.png"];
defURL = [NSURL fileURLWithPath:filePath];
}
if (defURL!=nil)
{
UIImage *poster = [[ImageLoader sharedLoader] loadImmediateImage:defURL withSize:imageSize];

// TODO: Use the full image size here? Auto width/height is going to be changed once the image is loaded.
autoWidth = poster.size.width;
autoHeight = poster.size.height;
[self imageView].image = poster;
}
[self loadDefaultImage:imageSize];
placeholderLoading = YES;
[(TiUIImageViewProxy *)[self proxy] startImageLoad:url_];
return;
}

if (image!=nil)
{
[(TiUIImageViewProxy*)[self proxy] setImageURL:url_];
Expand All @@ -473,10 +498,6 @@ -(void)loadUrl:(id)img
[self imageView].image = image;
[self fireLoadEventWithState:@"url"];
}
else
{
NSLog(@"[ERROR] couldn't find image for ImageView at: %@",img);
}
}
}

Expand Down Expand Up @@ -519,31 +540,6 @@ -(UIImage*)convertToUIImage:(id)arg
image = [[ImageLoader sharedLoader] loadImmediateImage:fileUrl withSize:CGSizeMake(TiDimensionCalculateValue(width, autoWidth),
TiDimensionCalculateValue(height, autoHeight))];
}
else if ([arg isKindOfClass:[NSString class]]) {
NSURL *url_ = [TiUtils toURL:arg proxy:self.proxy];

// TODO: Move this over into ImageLoader or some other way to more intellegently cache large files.
UIImage * testImage = [UIImage imageWithContentsOfFile:[url_ path]];
if (testImage != nil)
{
CGSize fullSize = [testImage size];
autoHeight = fullSize.height;
autoWidth = fullSize.width;
return testImage;
}
// END TODO

CGSize fullSize = [[ImageLoader sharedLoader] fullImageSize:url_];
autoHeight = fullSize.height;
autoWidth = fullSize.width;

image = [[ImageLoader sharedLoader] loadImmediateImage:url_ withSize:CGSizeMake(TiDimensionCalculateValue(width, autoWidth),
TiDimensionCalculateValue(height, autoHeight))];

if (image != nil) {
[(TiUIImageViewProxy*)[self proxy] setImageURL:url_];
}
}
else if ([arg isKindOfClass:[UIImage class]])
{
// called within this class
Expand Down Expand Up @@ -661,7 +657,7 @@ -(void)setImage_:(id)arg

BOOL replaceProperty = YES;
UIImage *image = nil;
NSURL* imageURL = nil;
if ([arg isKindOfClass:[UIImage class]])
{
// called within this class
Expand All @@ -678,17 +674,14 @@ -(void)setImage_:(id)arg

if (image == nil)
{
if ([arg isKindOfClass:[NSString class]])
{
[self loadUrl:[NSURL URLWithString:arg]];
return;
}
if ([arg isKindOfClass:[NSURL class]])
{
[self loadUrl:arg];
return;
}
[self throwException:@"invalid image type" subreason:[NSString stringWithFormat:@"expected either TiBlob or TiFile, was: %@",[arg class]] location:CODELOCATION];
NSURL* imageURL = [[self proxy] sanitizeURL:arg];
if (![imageURL isKindOfClass:[NSURL class]]) {
[self throwException:@"invalid image type"
subreason:[NSString stringWithFormat:@"expected TiBlob, String, TiFile, was: %@",[arg class]]
location:CODELOCATION];
}

[self loadUrl:imageURL];
return;
}

Expand Down Expand Up @@ -792,6 +785,7 @@ -(void)imageLoadSuccess:(ImageLoaderRequest*)request image:(UIImage*)image
{
image = [self scaleImageIfRequired:image];
}

TiThreadPerformOnMainThread(^{
[self setURLImageOnUIThread:image];
}, NO);
Expand Down

0 comments on commit db2ba90

Please sign in to comment.