对iphone应用程序加载图像的工具类 ImageHelper

#import


@interface ImageHelper : NSObject

@end


@interface ImageHelper (Files)

+ (UIImage *) imageNamed: (NSString *) aName;

+ (UIImage *) imageFromURLString: (NSString *) urlstring;

+ (NSArray *) DCIMImages;

+ (UIImage *) DCIMImageNamed: (NSString *) aName;

@end



#import "ImageHelper-Files.h"


@implementation ImageHelper

@end


NSString *documentsFolder()

{

return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

}


NSString *bundleFolder()

{

return [[NSBundle mainBundle] bundlePath];

}


NSString *DCIMFolder()

{

return [NSHomeDirectory() stringByAppendingPathComponent:@"../../Media/DCIM"];

}


@implementation ImageHelper (Files)


+ (NSString *) pathForItemNamed: (NSString *) fname inFolder: (NSString *) path

{

NSString *file;

NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:path];

while (file = [dirEnum nextObject]) 

if ([[file lastPathComponent] isEqualToString:fname]) 

return [path stringByAppendingPathComponent:file];

return nil;

}


// Searches bundle first then documents folder

+ (UIImage *) imageNamed: (NSString *) aName

{

NSString *path = [ImageHelper pathForItemNamed:aName inFolder:bundleFolder()];

path = path ? path : [ImageHelper pathForItemNamed:aName inFolder:documentsFolder()];

if (!path) return nil;

return [UIImage imageWithContentsOfFile:path];

}


+ (UIImage *) imageFromURLString: (NSString *) urlstring

{

NSURL *url = [NSURL URLWithString:urlstring];

return [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

}


+ (NSArray *) DCIMImages

{

NSString *file;

NSMutableArray *results = [NSMutableArray array];

NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:DCIMFolder()];

while (file = [dirEnum nextObject]) if ([file hasSuffix:@"JPG"]) [results addObject:file];

return results;

}


+ (UIImage *) DCIMImageNamed: (NSString *) aName

{

NSString *path = [ImageHelper pathForItemNamed:aName inFolder:DCIMFolder()];

return [UIImage imageWithContentsOfFile:path];

}

@end


你可能感兴趣的:(iphone,开发)