her i'm using this coding in my pr开发者_运维知识库ogram
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *imageCacheDirPath = [docsPath stringByAppendingPathComponent:@"Image"];
NSString *imageCachePath = [imageCacheDirPath stringByAppendingPathComponent:@"1.jpg"];
[[NSFileManager defaultManager] createFileAtPath:imageCachePath contents:dataObj attributes:nil];
what the problem in my coding.send some sample code .
One potential problem is that 'Image' directory does not exist when you try to create file in it - it's required for createFileAtPath method to run properly. So check if Image folder exists and create it if required:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *imageCacheDirPath = [docsPath stringByAppendingPathComponent:@"Image"];
NSString *imageCachePath = [imageCacheDirPath stringByAppendingPathComponent:@"1.jpg"];
if (![[NSFileManager defaultManager] fileExistsAtPath: imageCacheDirPath])
[[NSFileManager defaultManager] createDirectoryAtPath:imageCacheDirPath
withIntermediateDirectories:NO
attributes:nil
error:NULL];
[[NSFileManager defaultManager] createFileAtPath:imageCachePath
contents:dataObj
attributes:nil];
精彩评论