开发者

how to save image in subfolder (AnyName) in document in phone help me

开发者 https://www.devze.com 2023-03-01 23:38 出处:网络
her i\'m using this coding in my pr开发者_运维知识库ogram NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消