unityios相册_Unity中关于保存图片到Android/IOS相册中的问题

本文介绍了如何在Unity中实现截图并保存到Android和iOS设备的相册。对于Android,主要涉及截图后保存到指定路径的操作;而对于iOS,则需要借助Xcode中的方法来完成保存。详细步骤和代码示例分别给出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们在一些项目中可能需要截图保存功能(特别是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().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().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.youkuaiyun.com/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 ().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.cs

if (!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 ().enabled = true;

_demoBtns.SetActive (true);yield return new WaitForSeconds (1.2f);_SavePhoto (path_read);

}

}

Xcode中执行文件如下:

#import "

hotoManager.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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值