Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: bd1b4075ed
Fetching contributors…

Cannot retrieve contributors at this time

file 500 lines (427 sloc) 15.716 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 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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
//
// ShotLoader.m
// lifelapse
//
// Created by Brian King on 2/7/11.
// Copyright 2011 King Software Design. All rights reserved.
//

#import "Shot.h"
#import "BKImageStore.h"

static dispatch_queue_t __image_store_background_queue;

dispatch_queue_t image_store_background_queue()
{
    if (__image_store_background_queue == NULL)
    {
        __image_store_background_queue = dispatch_queue_create("com.ksd.image_store.background", 0);
    }
    return __image_store_background_queue;
}

static NSUInteger __loaderCount = 0;

#define CACHE_KEY(imageID, maxSize) [imageID stringByAppendingFormat:@"_%d", maxSize]

@interface BKImageStoreLoader : NSObject {
    NSUInteger _maxSize;
    bk_image_block _block;
    id _loaderID;
    BOOL _cancel;
}
- (id)initWithImageID:(NSString *)imageID size:(NSUInteger)maxSize block:(bk_image_block)block;

@property (readonly) NSString *imageID;
@property (readonly) id loaderID;
@property (readonly) NSUInteger maxSize;
@property (readonly) bk_image_block block;
@property (assign) BOOL cancel;
@end

@implementation BKImageStoreLoader
@synthesize imageID = _imageID;
@synthesize loaderID = _loaderID;
@synthesize maxSize = _maxSize;
@synthesize block = _block;
@synthesize cancel = cancel;
- (id)initWithImageID:(NSString *)imageID size:(NSUInteger)maxSize block:(bk_image_block)block;
{
    self = [super init];
    if (self)
    {
        _maxSize = maxSize;
        _block = [block copy];
        _imageID = [imageID copy];
        _loaderID = [[NSString stringWithFormat:@"ImageLoader-%d", __loaderCount++] retain];
        _cancel = NO;
    }
    return self;
}
- (NSString *)cacheKey
{
    return CACHE_KEY(_imageID, _maxSize);
}

- (void)dealloc
{
    [_block release];
    [_loaderID release];
    [_imageID release];
    [super dealloc];
}
@end

static BKImageStore *__imageStore = nil;

@implementation BKImageStore

@synthesize thumbnailSize = thumbnailSize_;
@synthesize storageSize = storageSize_;
@synthesize cacheFileIOErrorHandler = cacheFileIOErrorHandler_;
@synthesize jpegCompressionRatio = jpegCompressionRatio_;

+ (BKImageStore *)sharedStore
{
    if (__imageStore == nil)
    {
        __imageStore = [[BKImageStore alloc] init];
    }
    return __imageStore;
}

- (id)init
{
    self = [super init];
    if (self)
    {
        cache_ = [[NSCache alloc] init];
        imageBlockEntries_ = [[NSMutableDictionary alloc] init];
        cachedFileSizes_ = [[NSMutableSet alloc] init];
        thumbnailSize_ = NSNotFound;
        storageSize_ = NSNotFound;
        jpegCompressionRatio_ = 0.65;
        cacheFileIOErrorHandler_ = [^(NSError *error) {
            NSLog(@"Error saving a file to cache - %@", error);
        } copy];
    }
    return self;
}
- (void)dealloc
{
    [cache_ release];
    [imageBlockEntries_ release];
    [cacheFileIOErrorHandler_ release];
    [cachedFileSizes_ release];
    [super dealloc];
}

- (BOOL)preloadThumbnails
{
    return thumbnailSize_ != NSNotFound;
}

- (void)addCachedFileSize:(NSUInteger)size
{
    [cachedFileSizes_ addObject:[NSNumber numberWithUnsignedInteger:size]];
}
#pragma mark -
#pragma mark Path Accessors
- (NSString *)thumbnailPathForImageID:(NSString *)imageID
{
    NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    return [[[documents stringByAppendingPathComponent:@"Thumbnails"]
             stringByAppendingPathComponent:imageID] stringByAppendingPathExtension:@"jpg"];

}
- (NSString *)cachedPathForKey:(NSString *)key
{
    return [[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:key] stringByAppendingPathExtension:@"jpg"];
}

- (BOOL)cachedFileExistsForKey:(NSString *)key
{
    return [[NSFileManager defaultManager] fileExistsAtPath:[self cachedPathForKey:key]];
}

- (NSString *)sourcePathForImageID:(NSString *)imageID
{
    return [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:imageID] stringByAppendingPathExtension:@"jpg"];
}

- (BOOL)sourceFileExistsForImageID:(NSString *)imageID
{
    return [[NSFileManager defaultManager] fileExistsAtPath:[self sourcePathForImageID:imageID]];
}

#pragma mark -
#pragma mark Image Cache
- (UIImage *)cacheImageForKey:(NSString *)key
{
    return [cache_ objectForKey:key];
}
- (void)cacheImage:(UIImage *)image forKey:(NSString *)key
{
    [cache_ setObject:image forKey:key];
}

#pragma mark Image Entry Creation
- (BKImageStoreLoader *)loaderForBlock:(bk_image_block)block imageID:(NSString *)imageID sized:(NSUInteger)maxSize
{
    NSParameterAssert(block);
    NSParameterAssert(imageID);

    BKImageStoreLoader *loader = [[BKImageStoreLoader alloc] initWithImageID:imageID size:maxSize block:block];

    NSMutableDictionary *entries = [imageBlockEntries_ objectForKey:imageID];
    if (entries == nil)
    {
        entries = [NSMutableDictionary dictionary];
        [imageBlockEntries_ setObject:entries forKey:imageID];
    }
    [entries setObject:loader forKey:loader.loaderID];

    [loader release];
    return loader;
}

- (void)removeAllImageBlocksForImageID:(NSString *)imageID
{
    NSParameterAssert(imageID);
    for (BKImageStoreLoader *loader in [imageBlockEntries_ objectForKey:imageID])
        loader.cancel = YES;
    
    [imageBlockEntries_ removeObjectForKey:imageID];
}
- (void)removeImageBlockID:(id)loaderID forImageID:(NSString *)imageID;
{
    NSParameterAssert(imageID);
    // loaderID will be nil for cache hits
    if (loaderID == nil)
        return;
    
    NSMutableDictionary *entries = [imageBlockEntries_ objectForKey:imageID];
    BKImageStoreLoader *loader = [entries objectForKey:loaderID];
    loader.cancel = YES;
    [entries removeObjectForKey:loaderID];
    
    if ([entries count] == 0)
        [imageBlockEntries_ removeObjectForKey:imageID];

}
- (void)executeThumbnailForLoader:(BKImageStoreLoader *)loader
{
    UIImage *thumbnail = [self cacheImageForKey:CACHE_KEY(loader.imageID, self.thumbnailSize)];

    if (thumbnail)
        loader.block(thumbnail);
    else
        NSLog(@"No Thumbnail for imageID: %@", loader.imageID);
}
- (void)executeLoader:(BKImageStoreLoader *)loader withImage:(UIImage *)cachedImage;
{
    NSParameterAssert(loader);
    NSParameterAssert(cachedImage);
    //
    // Decompress the image off thread
    //
    UIGraphicsBeginImageContext(CGSizeMake(1, 1));
    [cachedImage drawAtPoint:CGPointZero];
    UIGraphicsEndImageContext();

    //
    // Execute the loader on the main thread and remove the loader
    //
    dispatch_async(dispatch_get_main_queue(), ^{
        //
        // If the loader has been cancelled, bail
        //
        if (loader.cancel)
            return;

        loader.block(cachedImage);
        
        NSMutableDictionary *entries = [imageBlockEntries_ objectForKey:loader.imageID];
        [entries removeObjectForKey:[loader loaderID]];
    });
}
- (BOOL)saveImageData:(NSData *)imageData toPath:(NSString *)path error:(NSError **)error
{
    NSString *directory = [path stringByDeletingLastPathComponent];
    
    BOOL ok = [[NSFileManager defaultManager] createDirectoryAtPath:directory
                                        withIntermediateDirectories:YES
                                                         attributes:nil
                                                              error:error];
    
    if (ok)
        ok = [imageData writeToFile:path options:NSDataWritingAtomic error:error];
    
    return ok;
}

- (void)saveToFile:(NSString *)path image:(UIImage *)image
{
    NSParameterAssert(path);
    NSParameterAssert(image);
    
    dispatch_async(image_store_background_queue(), ^{
        NSData *imageData = UIImageJPEGRepresentation(image, jpegCompressionRatio_);
        NSError *error = nil;
        
        BOOL ok = [self saveImageData:imageData toPath:path error:&error];
        if (!ok)
        {
            self.cacheFileIOErrorHandler(error);
        }
    });
}
- (UIImage *)imageFromCacheFile:(NSString *)path
{
    CFURLRef pathURL = (CFURLRef)[NSURL fileURLWithPath:path];
    CGImageSourceRef imageSource = CGImageSourceCreateWithURL(pathURL, NULL);
    
    CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
    UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];

    CGImageRelease(imageRef);
    return [image autorelease];
}
- (void)loadFromCacheFile:(BKImageStoreLoader *)loader
{
    NSParameterAssert(loader);
    
    dispatch_async(image_store_background_queue(), ^{
        //
        // Bail if we no longer have an image_block for this ID
        //
        if (loader.cancel) return;
        
        //
        // Load the resized image from disk
        //
        UIImage *image = [self imageFromCacheFile:[self cachedPathForKey:[loader cacheKey]]];
        //
        // Cache the image and execute any pending blocks
        //
        [self cacheImage:image forKey:[loader cacheKey]];
        [self executeLoader:loader withImage:image];
    });
}
- (CFDictionaryRef)resizeOptionsForMaxSize:(NSUInteger)maxSize
{
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
                             (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
                             (id)[NSNumber numberWithInt:maxSize], (id)kCGImageSourceThumbnailMaxPixelSize,
                             nil];
    return (CFDictionaryRef)options;
}

- (void)generateThumbnailFromImageSource:(CGImageSourceRef)imageSource forImageID:(NSString *)imageID
{
    NSParameterAssert(imageSource);
    NSParameterAssert(imageID);
    NSParameterAssert([self preloadThumbnails]);
    
    CGImageRef thumbnailImageRef =
        CGImageSourceCreateThumbnailAtIndex(imageSource,
                                            0,
                                            [self resizeOptionsForMaxSize:self.thumbnailSize]);
    UIImage *thumbnailImage = [[UIImage alloc] initWithCGImage:thumbnailImageRef];
    
    [self cacheImage:thumbnailImage forKey:CACHE_KEY(imageID, self.thumbnailSize)];
    [self saveToFile:[self thumbnailPathForImageID:imageID] image:thumbnailImage];

    CGImageRelease(thumbnailImageRef);
    [thumbnailImage release];
}

- (void)loadThumbnailCacheWithImageIDs:(NSArray *)imageIDs
{
    for (NSString *imageID in imageIDs)
    {
        NSString *path = [self thumbnailPathForImageID:imageID];
        UIImage *thumbnailImage = [self imageFromCacheFile:path];
        if (thumbnailImage)
            [self cacheImage:thumbnailImage forKey:CACHE_KEY(imageID, self.thumbnailSize)];
        else
        {
            //
            // Something failed when generating a thumbnail, so lets re-generate.
            //
            CFURLRef pathURL = (CFURLRef)[NSURL fileURLWithPath:[self sourcePathForImageID:imageID]];
            CGImageSourceRef imageSource = CGImageSourceCreateWithURL(pathURL, NULL);
            
            [self generateThumbnailFromImageSource:nil forImageID:imageID];

            CFRelease(imageSource);
        }

    }
}

- (void)resizeImageSourceRef:(CGImageSourceRef)imageSource loader:(BKImageStoreLoader *)loader
{
    NSParameterAssert(imageSource);
    NSParameterAssert(loader);

    CFRetain(imageSource);
    dispatch_async(image_store_background_queue(), ^{
        //
        // Bail if we no longer have an image_block for this ID
        //
        if (loader.cancel)
        {
            CFRelease(imageSource);
            return;
        }
        
        //
        // Resize the image
        //
        CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(imageSource,
                                                                  0,
                                                                  [self resizeOptionsForMaxSize:loader.maxSize]);
        UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
        
      
        //
        // Cache the image and execute any pending blocks
        //
        [self cacheImage:image forKey:[loader cacheKey]];
        [self executeLoader:loader withImage:image];
        
        //
        // Save the image if flagged as a 'cached size', and then a thumbnail
        //
        if ([cachedFileSizes_ containsObject:[NSNumber numberWithUnsignedInteger:loader.maxSize]])
            [self saveToFile:[self cachedPathForKey:[loader cacheKey]] image:image];

        CGImageRelease(imageRef);
        [image release];

        CFRelease(imageSource);
    });
}

- (id)addLoaderForImageID:(NSString *)imageID sized:(NSUInteger)maxSize block:(bk_image_block)block
{
    NSParameterAssert(imageID);
    NSParameterAssert(block);
    NSString *key = CACHE_KEY(imageID, maxSize);
    UIImage *cachedImage = [self cacheImageForKey:key];
    if (cachedImage)
    {
        block(cachedImage);
        return nil;
    }
    else
    {
        BKImageStoreLoader *loader = [self loaderForBlock:block imageID:imageID sized:maxSize];

        if ([self preloadThumbnails])
            [self executeThumbnailForLoader:loader];

        //
        // Load the cached resize if it exists, otherwise kick off the resize
        //
        if ([self cachedFileExistsForKey:key])
        {
            [self loadFromCacheFile:loader];
        }
        else if ([self sourceFileExistsForImageID:imageID])
        {
            CFURLRef pathURL = (CFURLRef)[NSURL fileURLWithPath:[self sourcePathForImageID:imageID]];
            CGImageSourceRef imageSource = CGImageSourceCreateWithURL(pathURL, NULL);

            [self resizeImageSourceRef:imageSource loader:loader];
     
            CFRelease(imageSource);
        }
        return loader.loaderID;
    }
}

- (void)saveImageID:(NSString *)imageID
           withData:(NSData*)imageData
             onSave:(bk_save_block)saveBlock
            onError:(bk_error_block)errorBlock
{
    NSParameterAssert(imageID);
    NSParameterAssert(imageData);
  dispatch_async(image_store_background_queue(), ^{
        //
        // Save Image to disk, creating any needed directories before hand
        //
        NSString *path = [self sourcePathForImageID:imageID];
        NSError *error = nil;
        BOOL ok = [self saveImageData:imageData toPath:path error:&error];
        if (!ok)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                errorBlock(error);
                [self removeAllImageBlocksForImageID:imageID];
            });
            return;
        }
        // FIXME: How do I determine the size of imageData without loading too much data so I can resize !

        dispatch_async(dispatch_get_main_queue(), ^{
            saveBlock();
        });
            
        CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);

        if ([self preloadThumbnails])
        {
            [self generateThumbnailFromImageSource:imageSource forImageID:imageID];
        }

        //
        // Kick off resizes in the main thread for any pending BKImageStoreLoader objects
        //
        dispatch_async(dispatch_get_main_queue(), ^{
            NSArray *loaders = nil;

            NSMutableDictionary *entries = [imageBlockEntries_ objectForKey:imageID];
            loaders = [entries allValues];

            // Kick off resizes for any block loaders in the system
            for (BKImageStoreLoader *loader in loaders)
            {
                [self resizeImageSourceRef:imageSource loader:loader];
            }
            CFRelease(imageSource);
        });
    });
}

@end
Something went wrong with that request. Please try again.