美文网首页
cocoscreator上传头像ios

cocoscreator上传头像ios

作者: 神大人korose | 来源:发表于2020-01-16 11:14 被阅读0次

oc头文件 ImagePickerViewController.h

#import <UIKit/UIKit.h>
 
@interface ImagePickerViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
    NSString* filePath;
}
 
+ (ImagePickerViewController *)getInstance;
 
// 打开本地相册
+ (void)openPhoto:(NSString *)width height:(NSString *)height quality:(NSString *)quality;
 
// 打开相机
+ (void)openCamera:(NSString *)width height:(NSString *)height quality:(NSString *)quality;
 
+ (NSString *)getNSHomeDirectory;
 
// base64 string to png
+ (void)base64StringToFile:(NSString *)base64String fileName:(NSString *)fileName;
 
@end

oc逻辑 ImagePickerViewController.mm

#import "ImagePickerViewController.h"
#import "cocos2d.h"
 
#include "../../../cocos2d-x/cocos/scripting/js-bindings/jswrapper/SeApi.h"
 
 
@implementation ImagePickerViewController{
    
}
 
static ImagePickerViewController* imagePickerViewController_instance;
static int imagePickerViewController_width;
static int imagePickerViewController_height;
static int imagePickerViewController_quality;
 
+ (ImagePickerViewController*)getInstance {
    if (imagePickerViewController_instance == nil) {
        imagePickerViewController_instance = [[ImagePickerViewController alloc] initWithNibName:nil bundle:nil];
    }
    return imagePickerViewController_instance;
}
 
+ (void)openPhoto:(NSString *)width height:(NSString *)height quality:(NSString *)quality {
    NSLog(@"调相册");
    imagePickerViewController_width = [width intValue];
    imagePickerViewController_height = [height intValue];
    imagePickerViewController_quality = [quality intValue];
    [imagePickerViewController_instance localPhoto];
}
 
+ (void)openCamera:(NSString *)width height:(NSString *)height quality:(NSString *)quality {
    NSLog(@"调相机");
    imagePickerViewController_width = [width intValue];
    imagePickerViewController_height = [height intValue];
    imagePickerViewController_quality = [quality intValue];
    [imagePickerViewController_instance takePhoto]; 
}
 
- (void) presentView:(UIImagePickerController *)picker {
    NSLog(@"不知道");
    if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
        NSLog(@"不知道1");
        [self presentViewController:picker animated:YES completion:nil];
    } else {
        NSLog(@"不知道2");
        [self presentModalViewController:picker animated:YES];
    }
}
 
-(void)localPhoto{
    NSLog(@"相册");
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate      = self;
    picker.sourceType    = sourceType;
    picker.allowsEditing = YES;
    [self presentView:picker];
}
 
-(void)takePhoto{
    NSLog(@"相机");
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController* picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图像可编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        
        [self presentView:picker];
    }
    else{
        NSLog(@"模拟器中无法打开照相机,请在真机中调试");
    }
}
 
// 获取保存图片的 IOS 路径
+ (NSString *)getNSHomeDirectory {
    NSLog(@"获取保存图片的 IOS 路径");
    return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
}
 
// base64 string to png
+ (void)base64StringToFile:(NSString *)base64String fileName:(NSString *)fileName {
    NSLog(@"base64转换");
    NSData *data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
    [data writeToFile:fileName atomically:YES];
}
 
void callJavaScript(const char *params) {
    NSLog(@"返回客户端");
//    jsval outVal;
//    se::ScriptEngine::getInstance()->evalString(params, &outVal);
    se::ScriptEngine::getInstance()->evalString(params);

}
 
// 压缩图片
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    NSLog(@"压缩图片");
    // 设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
 
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
 
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
 
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
 
    // 返回新的改变大小后的图片
    return scaledImage; 
 
}
 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSLog(@"进来了哦");
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        NSLog(@"当选择的类型是图片");
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
        image = [self scaleToSize:image size:CGSizeMake(imagePickerViewController_width, imagePickerViewController_height)];
        NSData *data = UIImageJPEGRepresentation(image, imagePickerViewController_quality / 100);
        
        NSString *base64Encoded = [data base64EncodedStringWithOptions:0];
        
        callJavaScript([[NSString stringWithFormat:@"playerIconLayerObtainTextureBase64String('%@')", base64Encoded] UTF8String]);
        
        //关闭相册界面
        [self imagePickerControllerDidCancel:picker];
    }
    
}
 
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    if ([picker respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
        [picker dismissViewControllerAnimated:YES completion:nil];    //iOS 5 crashes only if presentation was not animated
    } else {
        [picker dismissModalViewControllerAnimated:YES];    //deleting the previous condition, iOS 5 still crashes if presentation was not animated
    }
}
 
@end

初始化

#import "ImagePickerViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // init ImagePickerViewController viewController
    ImagePickerViewController *imagePicker = [ImagePickerViewController getInstance];
    [_viewController.view addSubview: imagePicker.view];
    [imagePicker.view setHidden:YES];
}

js调用方法
self.headImage.node.width为裁剪宽度,
self.headImage.node.height为裁剪高度,

    // 分平台 调用 访问相册
    onVisitPhoto : function (self) {
      if (cc.sys.os === cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod("ImagePickerViewController", "openPhoto:height:quality:",
                self.headImage.node.width, self.headImage.node.height, 80);
        }
    },

    // 分平台 调用 使用相机
    onUseCamera : function (self) {
      if (cc.sys.os === cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod("ImagePickerViewController", "openCamera:height:quality:",
                self.headImage.node.width, self.headImage.node.height, 80);
        }
    },

相关文章

网友评论

      本文标题:cocoscreator上传头像ios

      本文链接:https://www.haomeiwen.com/subject/vulfzctx.html