---
title: "ios文件读写"
author: "zcw_java"
date: "2013-08-12T15:44:26Z"
tags: []
categories: ["移动开发"]
slug: "ios"
draft: false
lastmod: "2013-08-12T15:44:26Z"
id: 1923522
url: "https://zcw-java.iteye.com/blog/1923522"
---

```c++

//写入缓存文件,缓存目录下
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *strDocumentsDirectory = paths[0];
    NSString *strCacheDirectoryName = [strdocumentsdirectory stringbyappendingpathcomponent:[nsstring stringwithformat:@"%@_alarm",@"2000000022"]];
    
    NSString *strURL = @"http://farm8.static.flickr.com/7443/9493141210_bfda8b9572_s.jpg";
    NSError *error = nil;
    NSData *dataImg = [nsdata datawithcontentsofurl:[nsurl urlwithstring:strurl] options:NSDataReadingMappedIfSafe error:&error];
    if ([error code] == 0)
    {
        if([[nsfilemanager defaultmanager] fileExistsAtPath:strCacheDirectoryName])
        {
            NSLog(@"文件夹存在");
        //创建文件夹路径
        [[nsfilemanager defaultmanager] createDirectoryAtPath:strCacheDirectoryName withIntermediateDirectories:YES attributes:nil error:nil];
        
        NSString *imagePath =[strcachedirectoryname stringbyappendingpathcomponent:@"2000000022_01.jpg"];
        //图片写入文件夹
        [uiimagepngrepresentation([uiimage imagewithdata:dataimg]) writeToFile:imagePath atomically:YES];
        }
        else
        {
            NSLog(@"不存在该文件夹");
        }
    }

```