Skip to content

Instantly share code, notes, and snippets.

@marshluca
Created December 17, 2010 04:32
Show Gist options
  • Save marshluca/744491 to your computer and use it in GitHub Desktop.
Save marshluca/744491 to your computer and use it in GitHub Desktop.
MUtility
//
// MUtility.m
// Magazine
//
// Created by wang xuefeng on 10-12-9.
// Copyright 2010 www.5yi.com. All rights reserved.
//
#import "MUtility.h"
#import "MFoundation.h"
#import "ZipArchive/ZipArchive.h"
@implementation MUtility
+ (UIColor *)randomColor
{
static BOOL seeded = NO;
if (!seeded) {
seeded = YES;
srandom(time(NULL));
}
CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
+ (CGFloat)getStringHeight:(NSString *)string font:(UIFont *)font withMaxSize:(CGSize)maxSize
{
CGSize size = [string sizeWithFont:font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
+ (NSString *)documentPathForFile:(NSString *)fileName
{
//NSLog(@"documents: %@",NSHomeDirectory());
return [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",fileName]];
}
+ (NSMutableArray *) getDataArrayFromFile:(NSString *)fileName
{
NSString *filePath = [MUtility documentPathForFile:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath]) {
NSMutableArray *initArray = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
[initArray writeToFile:filePath atomically:YES];
}
return [[NSMutableArray alloc] initWithContentsOfFile:filePath];
}
+ (void)saveToLibraryWithData:(NSDictionary *)dict
{
NSString *filePath = [MUtility documentPathForFile:@"library.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableArray *dataArray;
if (![fileManager fileExistsAtPath:filePath]) {
dataArray = [[NSMutableArray alloc] initWithCapacity:0];
} else {
dataArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
}
NSString *book_id = [NSString stringWithFormat:@"%d",[[dict objectForKey:@"id"] intValue]];
NSString *magazine_id = [NSString stringWithFormat:@"%d",[[dict objectForKey:@"magazine_id"] intValue]];
NSString *book_title = [NSString stringWithFormat:@"%@",[dict objectForKey:@"name"]];
NSString *book_cover = [NSString stringWithFormat:@"cover.png"];
NSString *book_catalog = [NSString stringWithFormat:@"catalog.plist"];
NSString *book_directory = [NSString stringWithFormat:@"book1%d",[[dict objectForKey:@"id"] intValue]];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
book_id,@"book_id",
magazine_id,@"magazine_id",
book_title,@"book_title",
book_cover,@"book_cover",
book_catalog,@"book_catalog",
book_directory,@"book_directory",nil];
[dataArray addObject:dictionary];
[dataArray writeToFile:filePath atomically:YES];
[dataArray release];
}
+ (void)unzipFileWithItem:(NSDictionary *)item
{
// .../Documents/book.zip
NSString *zipFilePath = [MUtility documentPathForFile:[item objectForKey:@"zip_name"]];
// .../Documents/book
NSString *zipFileDirectory = [MUtility documentPathForFile:[item objectForKey:@"directory_name"]];
// uncompress
ZipArchive* zipFile = [[ZipArchive alloc] init];
[zipFile UnzipOpenFile:zipFilePath];
[zipFile UnzipFileTo:zipFileDirectory overWrite:YES];
[zipFile UnzipCloseFile];
[zipFile release];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment