之前只写过安卓保存图片到相册,iOS和安卓目前对于保存到相册的操作是很类似的,只不过是和Unity的交互方式是不一样的,废话不多说,下面上代码。文章源自大腿Plus-https://www.shijunzh.com/archives/1737
首先是头文件文章源自大腿Plus-https://www.shijunzh.com/archives/1737
// [!] 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文件文章源自大腿Plus-https://www.shijunzh.com/archives/1737
#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.shijunzh.com/archives/1737
评论