1、獲取沙盒路徑
NSString *path_document=NSHomeDirectory();//設置存儲文件路徑?。。。。。∥募窂降拿忠欢ㄒ獏^分開 NSString *imagePath=[path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@%@%@.png",_shopObj.shopPicture,_shopObj.shopColor,self.shopObj.shopName]];//寫入文件 [UIImagePNGRepresentation(img) writeToFile:imagePath atomically:YES];//將地址存儲到自己設置的Model模型中 self.shopObj.shopPicture=imagePath;
2、讀取數據的時候,直接從自己設置文件中讀取出來就行了,例子如下:
NSString *path_document=NSHomeDirectory(); NSString *imagePath=[path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@%@%@.png",_shopObj.shopPicture,_shopObj.shopColor,self.shopObj.shopName]]; UIImage *img=[UIImage imageWithContentsOfFile:imagePath]; cell.imgV.image=img;
順便說一下設置的根目錄的位置:
2,獲取Documents目錄路徑的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *docDir = [paths objectAtIndex:0];
3,獲取Caches目錄路徑的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);NSString *cachesDir = [paths objectAtIndex:0];
4,獲取tmp目錄路徑的方法:
NSString *tmpDir = NSTemporaryDirectory();
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答