之前只写过安卓保存图片到相册,iOS和安卓目前对于保存到相册的操作是很类似的,只不过是和Unity的交互方式是不一样的,废话不多说,下面上代码。
首先是头文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// [!] important set UnityFramework in Target Membership for this file // [!] and set Public header visibility #import <Foundation/Foundation.h> // NativeCallsProtocol defines protocol with methods you want to be called from managed @protocol NativeCallsProtocol @required - (void) showHostMainWindow:(NSString*)msg; - (NSString*) getAppData:(NSString*)key; @end __attribute__ ((visibility("default"))) @interface FrameworkLibAPI : NSObject // call it any time after UnityFrameworkLoad to set object implementing NativeCallsProtocol methods +(void) registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi; @end @interface PhotoManager : NSObject - ( void ) imageSaved: ( UIImage *) image didFinishSavingWithError:( NSError *)error contextInfo: ( void *) contextInfo; @end |
接着是.mm文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#import <Foundation/Foundation.h> #import "NativeCallProxy.h" @implementation FrameworkLibAPI id<NativeCallsProtocol> api = NULL; +(void) registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi { api = aApi; } @end @implementation PhotoManager - ( void ) imageSaved: ( UIImage *) image didFinishSavingWithError:( NSError *)error contextInfo: ( void *) contextInfo { NSLog(@"保存结束"); if (error != nil) { NSLog(@"有错误"); } } @end extern "C" { //退出unity显示原生界面 void showHostMainWindow(const char* msg) { UnityPause(true); return [api showHostMainWindow:[NSString stringWithUTF8String:msg]]; } //获取原生界面数据 const char* getAppData(const char* key) { NSString* data = [api getAppData:[NSString stringWithUTF8String:key]]; return strdup([data cStringUsingEncoding:NSUTF8StringEncoding]); } 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]); PhotoManager *instance = [PhotoManager alloc]; UIImageWriteToSavedPhotosAlbum(img, instance, @selector(imageSaved:didFinishSavingWithError:contextInfo:), nil); } } |
iOS的这个工具类也会持续更新。敬请期待!文章源自大腿Plus-https://www.zhaoshijun.com/archives/1737 文章源自大腿Plus-https://www.zhaoshijun.com/archives/1737
我的微信
微信扫一扫
shijun_z
我的QQ
QQ扫一扫
846207670
评论