멍멍이네 블로그

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
CaptureHelper.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));

이후 카메라롤에서 캡쳐된 이미지를 확인하실수 있다.

 

 

 

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]);
    }
}