【IOS】关于截图并保存到IOS相册中

时间:2021-12-09 13:30:22

文章参考至:http://blog.csdn.net/wf_unity/article/details/22087643

C# 脚本

进行截图

Invoke ("ShowSaveObj", 0.5f);
showSaveObj<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">是项目中的方法 可以删掉</span>

filename = Application.persistentDataPath + "/Screenshot.png";

Texture2D screenShot = new Texture2D (Screen.width, Screen.height, TextureFormat.RGB24, false);

		//screenShot.ReadPixels(rect, 0, 0);  
		screenShot.ReadPixels (new Rect (0, 0, Screen.width, Screen.height), 0, 0);
		// 然后将这些纹理数据,成一个png图片文件  
		byte[] bytes = screenShot.EncodeToJPG ();
		screenShot.Compress (true);
		screenShot.Apply ();  

		//string destination = "/sdcard/DCIM/Camera";
		
		//string filename = Application.persistentDataPath + "/icon.png";  
		//string filename = destination + "/icon.png";
		System.IO.File.WriteAllBytes (filename, bytes);  
		Debug.Log (string.Format ("截屏了一张图片: {0}", filename));  
			
		Invoke ("ShowSaveObj", 0.5f);
		#if UNITY_IPHONE
		_SavePhoto (filename);	
		#endif

下面需要在xcode 创建.h和.m文件

目录是Plugins下的ios

.h

//
//  ViewController.h
//  IOSItem
//
//  Created by ldy on 16/8/12.
//  Copyright © 2016年 ldy. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LdyController : NSObject

- ( void ) imageSaved: ( UIImage *) image didFinishSavingWithError:( NSError *)error
          contextInfo: ( void *) contextInfo;

@end

.m
//
//  ViewController.m
//  IOSItem
//
//  Created by ldy on 16/8/12.
//  Copyright © 2016年 ldy. All rights reserved.
//

#import "LdyController.h"

@interface LdyController ()

@end

@implementation LdyController

- ( void ) imageSaved: ( UIImage *) image didFinishSavingWithError:( NSError *)error
          contextInfo: ( void *) contextInfo
{
    NSLog(@"保存结束");
    if (error != nil) {
        NSLog(@"有错误");
    }
}
void _SavePhoto(char *readAddr)
{
    NSString *strReadAddr = [NSString stringWithUTF8String:readAddr];
    UIImage *img = [UIImage imageWithContentsOfFile:strReadAddr];
    LdyController *instance = [LdyController alloc];
    UIImageWriteToSavedPhotosAlbum(img, instance,@selector(imageSaved:didFinishSavingWithError:contextInfo:), nil);
}

@end