[Unity3D/iOS] Unity3D에서 iOS기반 사진 캡쳐 및 카메라 엘범 사용
Unity - iOS 접근 플러그인 방법
제목 : Unity for IOS_Screen Capture한 이미지 카메라롤 에 등록하기.
Explan.
IOS에서 Application.CaptureScreenShot(path) 메소드를 사용할경우
캡처는 진행되나 해당 app 의 document폴더로 들어가서 카메라 롤에서 확인할수가 없다.
이를 등록해주는 방법 포스팅.
CaptureHelper.h
#import <Foundation/Foundation.h> @interface CaptureHelper : NSObject +(CaptureHelper *)sharedInstancs; @end
#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));
이후 카메라롤에서 캡쳐된 이미지를 확인하실수 있다.
URL : http://gomlib.blogspot.kr/2014/10/unity-for-iosscreen-capture.html
------------------------------------------------------------------------------
제목 : [iOS] 이미지, 동영상을 카메라 롤에 저장
이미지
|
1
2
3
4
5
6
7
8
9
10
11
12
13 |
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];// 이미지를 카메라 롤에 저장UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);// 저장한 이후에 실행 될 메소드- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { if (error) { NSLog(@"error: %@", [error localizedDescription]); } else { NSLog(@"saved"); }} |
동영상
|
1
2
3
4
5
6
7
8
9
10
11
12
13 |
NSURL *mediaUrl = [info objectForKey:UIImagePickerControllerMediaURL];// 동영상을 카메라 롤에 저장UISaveVideoAtPathToSavedPhotosAlbum([mediaUrl path], self, @selector(video:didFinishSavingWithError:contextInfo:), nil);// 저장한 이후에 실행 될 메소드- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { if (error) { NSLog(@"error: %@", [error localizedDescription]); } else { NSLog(@"saved"); }} |
출처 URL : http://fra3il.tistory.com/66
--------------------------------------------------------------------------------------
-(void) GetImageFromAlbum {
[self GetImage:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
}
-(void) StartCameraImagePic {
NSLog(@"StartCameraImagePic");
[self GetImage:UIImagePickerControllerSourceTypeCamera];
}
-(void) GetImageFromCamera {
BOOL cameraAvailableFlag = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if (cameraAvailableFlag) {
[self performSelector:@selector(StartCameraImagePic) withObject:nil afterDelay:0.9];
}
}
-(void) GetImage: (UIImagePickerControllerSourceType )source {
UIViewController *vc = UnityGetGLViewController();
if(_imagePicker == NULL) {
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.delegate = self;
}
_imagePicker.sourceType = source;
[vc presentViewController:_imagePicker animated:YES completion:nil];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIViewController *vc = UnityGetGLViewController();
[vc dismissViewControllerAnimated:YES completion:nil];
// added video support
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType]; // get media type
// if mediatype is video
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
NSString *moviePath = [videoUrl path];
UnitySendMessage("IOSCamera", "OnVideoPickedEvent", [ISNDataConvertor NSStringToChar:moviePath]);
} else{
// it must be an image
UIImage *photo = [info objectForKey:UIImagePickerControllerOriginalImage];
NSString *encodedImage = @"";
if (photo == nil) {
NSLog(@"no photo");
} else {
// NSLog(@"MaxImageSize: %i", [self MaxImageSize]);
// NSLog(@"photo.size.width: %f", photo.size.width);
if(photo.size.width > [self MaxImageSize] || photo.size.height > [self MaxImageSize] ) {
NSLog(@"resizing image");
CGSize s = CGSizeMake([self MaxImageSize], [self MaxImageSize]);
if(photo.size.width > photo.size.height) {
CGFloat new_height = [self MaxImageSize] / (photo.size.width / photo.size.height);
s.height = new_height;
} else {
CGFloat new_width = [self MaxImageSize] / (photo.size.height / photo.size.width);
s.width = new_width;
}
photo = [ISNCamera imageWithImage:photo scaledToSize:s];
}
NSData *imageData = nil;
NSLog(@"ImageCompressionRate: %f", [self ImageCompressionRate]);
if([self encodingType] == 0) {
imageData = UIImagePNGRepresentation(photo);
} else {
imageData = UIImageJPEGRepresentation(photo, [self ImageCompressionRate]);
}
encodedImage = [imageData base64Encoding];
}
UnitySendMessage("IOSCamera", "OnImagePickedEvent", [ISNDataConvertor NSStringToChar:encodedImage]);
}
}
'프로그래밍 > Unity3D' 카테고리의 다른 글
| [Unity3D/FacebookAPI] Unable to verify assembly data; you must provide an authorization key when loading this assembly. (0) | 2015.08.26 |
|---|---|
| [Unity3D] 드로우 콜 (Draw Call)에 관하여 (0) | 2015.08.19 |
| [Unity3D] 유니티에서 드래그로 오브젝트 선택하는거 만들기 영상 (0) | 2015.07.06 |
| [Unity3D] 유니티 이벤트 함수의 실행 순서 (0) | 2015.07.01 |
| [Unity3D/C#] 어플리케이션 홈키와 종료 메소드 (0) | 2015.06.26 |