iPhone和symbian 3rd一樣,會為每一個應用程序生成一個私有目錄,這個目錄位于/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications下,并隨即生成一個數字字母串作為目錄名,在每一次應用程序啟動時,這個字母數字串都是不同于上一次。
通常使用Documents目錄進行數據持久化的保存,而這個Documents目錄可以通過 NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES) 得到,代碼如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// NSString *path = [documentsDirectory stringByAppendingPathComponent:@"aa.plist"];
NSLog(@"path: %@",path);
打印結果如下:
path: /Users/apple/Library/Application Support/iPhone Simulator/4.3/Applications/550AF26D-174B-42E6-881B-B7499FAA32B7/Documents
而這個目錄還可以通過 NSHomeDirectory()來得到,代碼如下:
NSString *destPath = NSHomeDirectory();
NSLog(@"path: %@",destPath);
//destPath = [destPath stringByAppendingPathComponent: @"Documents"];
//NSString *xmlpath = [destPath stringByAppendingPathComponent: @"menu/menu.xml"];
打印結果如下:
path: /Users/apple/Library/Application Support/iPhone Simulator/4.2/Applications/6F4BC466-C5D6-440C-BAAC-BE20FA468C61
看看兩者打印出來的結果,我們可以看出這兩種方法的不同。
2. 瀏覽document下所有圖片資源
#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
NSArray *fileList = [[[NSFileManager defaultManager] directoryContentsAtPath:DOCUMENTS_FOLDER]
pathsMatchingExtensions:[NSArray arrayWithObject:@"png"]] ;
3. 得到圖片中的某一部分:
CGImageRef imageRef = image.CGImage;
CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);
CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);
UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];