IOS-写文件到本地,文件夹读取

-(void)writeToFileWithString:(NSString*)string withFileName:(NSString*)fileName{
   //@"Map_Succ.txt"
    NSString* fileName1 = [self filePath:fileName];
    NSFileManager* fileManager = [NSFileManager defaultManager];
    NSString* str = nil;
    if ( [fileManager fileExistsAtPath:fileName1]) {

        NSData* data = [NSData dataWithContentsOfFile:fileName1];
        str =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        string = [NSString stringWithFormat:@"%@\n%@",str,string];

    }

    [fileManager createFileAtPath:fileName1 contents:[ string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

}
- (NSString*)filePath:(NSString*)fileName {
    NSArray* myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* myDocPath = [myPaths objectAtIndex:0];
    NSString* filePath = [myDocPath stringByAppendingPathComponent:fileName];
    return filePath;
}
    NSData *data = UIImageJPEGRepresentation([UIImage imageWithCGImage:iam], 0.7);
    NSLog(@"%lu",(unsigned long)data.length);
      NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString* str =[self filePath:@"1.png"];
    [fileManager createDirectoryAtPath:str withIntermediateDirectories:YES attributes:nil error:nil];
     NSData* data = [NSData dataWithContentsOfFile:str];
   [fileManager createFileAtPath:[str stringByAppendingString:@"/image.png"] contents:data attributes:nil];
第一种  黄色文件夹下:

    NSString *ImagePath =[[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:@"惠购物icon.png"];

    self.imageTO.image = [UIImage imageWithContentsOfFile:ImagePath];

    NSLog(@"资源图片路径:%@",ImagePath);

第二种 蓝色文件夹下:

  NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"image"];
    if(themePath)
    {
        self.imageShow.image = [UIImage imageWithContentsOfFile:[themePath stringByAppendingPathComponent:@"惠购物icon.png"]];
    }
    NSLog(@"蓝色文件夹下图片的路径:%@",[themePath stringByAppendingPathComponent:@"惠购物icon.png"]);

你可能感兴趣的:(IOS-写文件到本地,文件夹读取)