IOS 开发获取本地图片路径及上传
程序员文章站
2023-12-22 21:05:10
1、获取沙盒路径
nsstring *path_document=nshomedirectory();
//设置存储文件路径!!!!!!文件路径的名字一定...
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();
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!