Unity3D之IOS Document

很多unity开发者比较纠结IOS document操作,本文将以读取document下的图片为例,实现Document扫盲,此篇为下一篇续集,document下的图片是拍照完成后保存的。保存图片到document操作如下:

复制代码
-(void)SavePngToDocument:(UIImage*)img{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);//获取document路径
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"pic.png"];//拼接字符串 图片名称为pic
    NSData *imageData = UIImagePNGRepresentation(img);//UIImage到NSData格式转换
    [imageData writeToFile:savedImagePath atomically:NO];//生成图片
}
复制代码

unity3d中通过如下操作获取Document文件路径

复制代码
    public string JsonPath
    {
        get{
            string    path=null;
            if(Application.platform==RuntimePlatform.IPhonePlayer)//判断平台
            {
                path= Application.dataPath.Substring (0, Application.dataPath.Length - 5);//ios 平台 就会获取documents路径
                path = path.Substring(0, path.LastIndexOf('/'))+"/Documents/";    
            }
            else
            {
                path=Application.dataPath+"/GameData/";//pc平台 获取当前工程GameData/的路径    GameData需要自己新建
            }
            return path;
        }     
    }
复制代码


通过WWW加载图片

复制代码
IEnumerator InstanceTexture(string fileName)
 {
        string path="file://"+JsonPath+fileName;
        print(path);
        WWW wwwTexture=new WWW(path);
        yield return wwwTexture;//注意 当用这种wwwTexture.texture 方式获取图片时,使用完了之后切记wwwTexture=null;  Resources.UnloadUnusedAssets() 否则大量使用时,内存一直增加。这样起到一个很好的回收作用
}
复制代码


另外介绍一款工具 ITools windows版可以查看app包的Document文件夹

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值