Explan.
IOS에서 Application.CaptureScreenShot(path) 메소드를 사용할경우
캡처는 진행되나 해당 app 의 document폴더로 들어가서 카메라 롤에서 확인할수가 없다.
이를 등록해주는 방법 포스팅.
CaptureHelper.h
#import <Foundation/Foundation.h> @interface CaptureHelper : NSObject +(CaptureHelper *)sharedInstancs; @endCaptureHelper.mm
#import "CaptureHelper.h"
static CaptureHelper * captureHelper = [CaptureHelper sharedInstancs];
@implementation CaptureHelper : NSObject
+ (void)initialize{
if(captureHelper == nil)
captureHelper = [[CaptureHelper alloc] init];
}
+ (CaptureHelper *)sharedInstancs{
return captureHelper;
}
- (id)init{
if(captureHelper != nil){
return captureHelper;
}
self = [super init];
if(self){
captureHelper = self;
}
return self;
}
- (NSString *)getDocumentDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
@end
extern "C"{
void CaptureToCameraRoll(const char *fileName)
{
NSString *file = [NSString stringWithUTF8String:(fileName)];
NSString *filePath = [[captureHelper getDocumentDirectory] stringByAppendingString:file];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
}
위 2개의 파일을 생성후 Plugin/IOS 폴더에 위치 그후
how to used.
[DllImport("__Internal")]
public static extern void CaptureToCameraRoll(String fileName);
// 그이후 스크린 캡처 진행시.
Application.CaptureScreenShot(path);
CaptureToCameraRoll(string.Format("/{0}",path));
이후 카메라롤에서 캡쳐된 이미지를 확인하실수 있다.
좋은 정보 감사합니다. 딱 필요했는데 덕분에 해결했습니다. ^^!!
답글삭제(이거 적용하고 iOS 빌드했더니 `DllImport' could not be found 가 발생해서
찾아보니 Unity 쪽에 using System.Runtime.InteropServices; 를 추가해줘야 된다고 하네요~
저와 같은 문제 겪으시는 분들께 도움 되시길! )
다른 분들을 위해 댓글 답니다.
답글삭제저 같은 경우는
--------
void CaptureToCameraRoll(const char *fileName)
{
NSString *file = [NSString stringWithUTF8String:(fileName)];
NSLog(@"%@", file);
//https://stackoverflow.com/questions/7525348/uiimage-initwithcontentsoffile-doesnt-work
//UIImage *image = [[UIImage alloc] initWithContentsOfFile:file];
UIImage *image = [UIImage imageWithContentsOfFile:file];
NSLog(@"%@", image);
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
--------
요렇게 했는데, 포인트는
- parameter로 넘겨준 fileName은 이미지 파일 이름이 포함된 전체 절대경로로 전달, 따라서 captureHelper가 필요 없어짐
- 예) var/mobile/Containers/Data/Application/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/Documents/screenshot.png
- UIImage 생성할 때 imageWithContentsOfFile 사용
요렇게 하니까 됐네요.