Skip to content

Instantly share code, notes, and snippets.

@vl4dimir
Created May 9, 2010 18:01
Show Gist options
  • Save vl4dimir/395311 to your computer and use it in GitHub Desktop.
Save vl4dimir/395311 to your computer and use it in GitHub Desktop.
#import "UIImage+Resizing.h"
@implementation Photo
@dynamic imagePath;
@dynamic thumbnailPath;
static const CGFloat kThumbnailWidth = 75.0f;
static const CGFloat kThumbnailHeight = 75.0f;
static const CGFloat kJPEGQuality = 0.85f;
#pragma mark -
#pragma mark Path generators
/**
* Generates a unique path in the photos folder for the image that is to be saved to disk.
* @return a generated path
*/
- (NSString*) generatePath
{
NSTimeInterval timeIntervalSeconds = [NSDate timeIntervalSinceReferenceDate];
unsigned long long nanoseconds = (unsigned long long) floor(timeIntervalSeconds * 1000000);
return [NSString stringWithFormat:@"Photos/%qu.jpg", nanoseconds];
}
/**
* Generates a full path to the image, since the imagePath property only stores the path relative to
* the Documents folder. The location of the application folder (and in turn, the Documents folder)
* changes on every app deployment. This function fetches the Documents folder location and appends
* the imagePath string to it, which results in a full path to the image.
*
* @return a full path to the image
*/
- (NSString*) fullImagePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [(NSString*)[paths objectAtIndex:0] stringByAppendingPathComponent:self.imagePath];
}
/**
* Generates a full path to the thumbnail. See fullImagePath description for details.
* @return a full path to the thumbnail
*/
- (NSString*) fullThumbnailPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [(NSString*)[paths objectAtIndex:0] stringByAppendingPathComponent:self.thumbnailPath];
}
#pragma mark -
#pragma mark Virtual image setter
/**
* This method creates a thumbnail from the given image, saves both the thumbnail and the image
* to disk, and updates thumbnailPath and imagePath accordingly.
*
* @param image The image to be saved to disk
*/
- (void) setImage:(UIImage*)image
{
UIImage* thumbnail = [image resizedImageWithSize:CGSizeMake(kThumbnailWidth, kThumbnailHeight)];
// Get autoreleased NSData representations
NSData* imageData = UIImageJPEGRepresentation(image, kJPEGQuality);
NSData* thumbnailData = UIImageJPEGRepresentation(thumbnail, kJPEGQuality);
// Generate image path and save the image
self.imagePath = [self generatePath];
if ([imageData writeToFile:[self fullImagePath] atomically:YES] == NO) {
// Saving failed, clean up and bail
self.imagePath = nil;
return;
}
// Generate thumbnail path and save the thumbnail
self.thumbnailPath = [self generatePath];
if ([thumbnailData writeToFile:[self fullThumbnailPath] atomically:YES] == NO) {
// Saving failed, clean up
[[NSFileManager defaultManager] removeItemAtPath:self.imagePath error:nil];
self.imagePath = nil;
self.thumbnailPath = nil;
// Bail
return;
}
}
#pragma mark -
#pragma mark Virtual getter methods
/**
* Virtual image getter method - returns an autoreleased UIImage object loaded from imagePath.
* @return an autoreleased image, or nil if there was an error
*/
- (UIImage*) image
{
if (self.imagePath == nil) {
return nil;
}
return [[[UIImage alloc] initWithContentsOfFile:[self fullImagePath]] autorelease];
}
/**
* Virtual thumbnail getter method - returns an autoreleased UIImage object loaded from thumbnailPath.
* @return an autoreleased thumbnail, or nil if there was an error
*/
- (UIImage*) thumbnail
{
if (self.thumbnailPath == nil) {
return nil;
}
return [[[UIImage alloc] initWithContentsOfFile:[self fullThumbnailPath]] autorelease];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment