我们在一些项目中可能需要截图保存功能(特别是AR的一些项目),将截下来的图保存的相册中,从而实现分享功能。下面就Android和IOS说一下他们是如何将图片保存到本地图册的。

关于安卓端,保存到相册方法很简单,就是也路径的问题,具体方法如下:

public class takephoto : MonoBehaviour{    private int i = 0;    //UI    public GameObject[] btn;    //存储路径    private string Path_save;    //读取路径    private string Path_read;    private string filepath;    private string destination;    void Start()    {        filepath = Application.persistentDataPath + "/test.txt";    }    void OnClick()    {        StartCoroutine(getTexture2d());    }    IEnumerator getTexture2d()    {        //隐藏UI        for (int j = 0; j < btn.Length; j++)        {            btn[j].GetComponentInChildren<UISprite>().enabled = false;        }        //截图操作        yield return new WaitForEndOfFrame();        Texture2D t = new Texture2D(Screen.width, Screen.height,TextureFormat.RGB24,false);        //显示UI        for (int j = 0; j < btn.Length; j++)        {            btn[j].GetComponentInChildren<UISprite>().enabled = true;        }        t.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);        byte[] bytes = t.EncodeToPNG();        t.Compress(true);        t.Apply();        //获取系统时间        System.DateTime now = new System.DateTime();        now = System.DateTime.Now;        string filename = string.Format("image{0}{1}{2}{3}.png", now.Day, now.Hour, now.Minute, now.Second);        //记录每一个截图名字        StreamWriter sw;        FileInfo ft = new FileInfo(filepath);        if (!ft.Exists)        {            sw = ft.CreateText();        }        else        {            sw = ft.AppendText();        }        sw.WriteLine(filename);        sw.Close();        sw.Dispose();        //应用平台判断,路径选择        if (Application.platform == RuntimePlatform.Android)        {            string origin = Path_save;            destination = "/mnt/sdcard/DCIM/ARphoto";            if (!Directory.Exists(destination))            {                Directory.CreateDirectory(destination);            }            destination = destination + "/" + filename;            Path_save = destination;               }        //保存文件        File.WriteAllBytes(Path_save, bytes);    }}



其中主要的就是安卓相册的一个路径问题,我是在相册路径下,新建了一个名叫ARphoto相册,所有保存下来的图片都保存在这个相册中,可以再图库应用中查看到这个相册文件夹。


在IOS端保存相册是无法单独在Unity中完成的。需要调用Xcode中的方法,关于Unity与Xcode之间的交互有什么疑问的可以查看我之前写的 博文http://blog.csdn.net/hasion/article/details/43668229

其具体实现方法如下:

public class TakePhoto : MonoBehaviour{[DllImport("__Internal")]private static extern void _SavePhoto (string readaddr);private string path_save;private string path_read;public GameObject _demoBtns;void OnClick (){this.GetComponentInChildren<UITexture> ().enabled = false;_demoBtns.SetActive (false);System.DateTime now = System.DateTime.Now;string filename = string.Format ("image{0}{1}{2}{3}{4}{5}.png", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);path_save = filename;path_read = Application.persistentDataPath + "/" + path_save;//set photomanager.csif (!string.IsNullOrEmpty (path_save)) {PhotosManager.GetInstance ().PhotoPath = path_read;}StartCoroutine (SavePhoto ());}IEnumerator SavePhoto (){yield return new WaitForSeconds (0.2f);Application.CaptureScreenshot (path_save);yield return new WaitForSeconds (0.5f);this.GetComponentInChildren<UITexture> ().enabled = true;_demoBtns.SetActive (true);yield return new WaitForSeconds (1.2f);_SavePhoto (path_read);}}

Xcode中执行文件如下:

#import "PhotoManager.h"@implementation PhotoManager- ( void ) imageSaved: ( UIImage *) image didFinishSavingWithError:( NSError *)error          contextInfo: ( void *) contextInfo{    if (error != nil)    {        NSLog(@"有错误");    }    else    {        NSLog(@"保存结束");    }}void _SavePhoto(char *readAddr){    NSString *strReadAddr = [NSString stringWithUTF8String:readAddr];    UIImage *img = [UIImage imageWithContentsOfFile:strReadAddr];    NSLog(@"%@",[NSString stringWithFormat:@"w:%f, h:%f", img.size.width, img.size.height]);    NSLog(@"%@",[NSString stringWithFormat:@"%s",readAddr ]);    PhotoManager *instance = [PhotoManager alloc];    UIImageWriteToSavedPhotosAlbum(img, instance,                                   @selector(imageSaved:didFinishSavingWithError:contextInfo:), nil);}@end


大体执行流程就是这样的,有什么错误或者建议的地方,欢迎大家指正,谢谢!!!





更多相关文章

  1. Pycharm安装PyQt5的详细教程
  2. android拍照与读取相册
  3. Android--SoLoader,android动态加载so库
  4. Android(安卓)命令行编译、打包生成apk文件
  5. android解决坚屏拍照和保存图片旋转90度的问题,并兼容4.0
  6. android手机客户端上传文件,java servlet服务器端接收并保存到服
  7. Android(安卓)获取存储卡路径和空间使用情况
  8. android Uri获取真实路径转换成File的方法
  9. android从服务器下载文件(php+apache+win7+MySql)

随机推荐

  1. Android 采用MD5注册登录,加密解密
  2. android sqlite 一次创建多个表
  3. eclipse 插件网址
  4. Android 监控网络状态
  5. Android全局异常统一处理
  6. android SharedFerence使用
  7. android 获取屏幕高度和宽度
  8. Android--WindowManager.LayoutParams(上
  9. android 模拟滑动事件
  10. Android:BT测试代码