cocos ios 相册访问

//
//  callSystemPhoto.h
//  hello_world-mobile
//

//

#import <Foundation/Foundation.h>

@interface callSystemPhoto : NSObject<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{

    
}

@end

// callSystemPhoto.mm


#import "callSystemPhoto.h"
#import <AVFoundation/AVMediaFormat.h>
#import<AssetsLibrary/AssetsLibrary.h>
#import <AVFoundation/AVCaptureDevice.h>
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"

@implementation callSystemPhoto

+(void)callPhoto{
    NSLog(@" callPhoto  ");
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        NSLog(@"支持相册");
        callSystemPhoto* systemPhoto = [[callSystemPhoto alloc]init];
        [systemPhoto showPhoto];
    }else{
        UIAlertView *alert = [[UIAlertView
                               alloc]initWithTitle:@"提示"
                              message:@"请在设置-->隐私-->照片,中开启本应用的相机访问权限!!"
                              delegate:self
                              cancelButtonTitle:@"取消"
                              otherButtonTitles:@"我知道了",
                              nil];
        [alert show];
    }
}
+(void)callCamera{
    NSLog(@" callCamera  ");
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        callSystemPhoto* systemPhoto = [[callSystemPhoto alloc]init];
        [systemPhoto getCameraAuthor];
    }else{
        UIAlertView *alert = [[UIAlertView
                               alloc]initWithTitle:@"提示"
                              message:@"请在设置-->隐私-->相机,中开启本应用的相机访问权限!!"
                              delegate:self
                              cancelButtonTitle:@"取消"
                              otherButtonTitles:@"我知道了",
                              nil];
        [alert show];
    }
   
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    NSLog(@"didFinishPickingMediaWithInfo");
//    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
   
//            NSString *imageUrl = [info valueForKey:UIImagePickerControllerReferenceURL]; //原始图片
//            NSLog(@"相片库 %@",imageUrl);
//         NSString *func = [NSString stringWithFormat:@"require('jsbScript').OcCallBack('%@')",imageUrl];
//        // 转为C风格字符串
//        const char *stringFunc = [func UTF8String];
//        //   jsval *outVal;
//        se::ScriptEngine::getInstance()->evalString(stringFunc);
        
     
//    }else if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
        NSString *type = [info objectForKey:@"UIImagePickerControllerMediaType"];
        if ([type isEqualToString:@"public.image"])
        {
            //先把图片转成NSData
            //原始图
            //        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
            
            UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
            NSData *data;
            if (UIImagePNGRepresentation(image) == nil)
            {
                data = UIImageJPEGRepresentation(image, 1.0);
            }
            else
            {
                data = UIImagePNGRepresentation(image);
            }
            
            //图片保存路径
            //这里将图片放在沙盒的documents/image文件夹中
            NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
            NSString *imgPath = [documentsPath stringByAppendingPathComponent:@"image"];
            
            //文件管理器
            NSFileManager *fileManager = [NSFileManager defaultManager];
            //生成唯一字符串
            NSString *uuid = [[NSUUID UUID]UUIDString];
            //生成文件名/Users/zhangchaoqing/Documents/mj_client/pokerMaster0/build/ios0/jsb-default/frameworks/runtime-src/proj.ios_mac/ios/WXApiManager.mm
            NSString *fileName = [NSString stringWithFormat:@"%@.png",uuid];
            
            //把刚刚由图片转成的data对象拷贝至沙盒中 并保存为xxxxx-xxxx-xxx...xxx.png
            /******保存之前最好先清空下,不然占用磁盘越来越大********/
            [fileManager removeItemAtPath:imgPath error:nil];
            /*************************************************/
            
            [fileManager createDirectoryAtPath:imgPath withIntermediateDirectories:YES attributes:nil error:nil];
            [fileManager createFileAtPath:[imgPath stringByAppendingPathComponent:fileName] contents:data attributes:nil];
            
            //得到选择后沙盒中图片的完整路径
           NSString* filePath = [[NSString alloc]initWithFormat:@"%@",[imgPath stringByAppendingPathComponent:fileName]];
            NSLog(@"%@",filePath);
            //通知ImageCrop完成取图
//            std::string strFilePath = [filePath UTF8String];
             NSString *func = [NSString stringWithFormat:@"require('jsbScript').NativePhotoUrl('%@')",filePath];
            // 转为C风格字符串
            const char *stringFunc = [func UTF8String];
            //   jsval *outVal;
            se::ScriptEngine::getInstance()->evalString(stringFunc);
        }
    
//    }
    
//    NSLog(@"%@",info.description);
    [picker dismissViewControllerAnimated:YES completion:nil];
}
//当用户取消选择的时候,调用该方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    NSLog(@"imagePickerControllerDidCancel");
    [picker dismissViewControllerAnimated:YES completion:nil];
}


-(void)getCameraAuthor{
    //相机权限
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus ==AVAuthorizationStatusRestricted ||//此应用程序没有被授权访问的照片数据。可能是家长控制权限
        authStatus ==AVAuthorizationStatusDenied)  //用户已经明确否认了这一照片数据的应用程序访问
    {
        // 无权限 引导去开启
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication]canOpenURL:url]) {
            [[UIApplication sharedApplication]openURL:url];
        }
    }else{
        [self showCamera];
    }
}

- (void)showCamera {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.delegate = self; //设置代理
    
    //是否允许编辑照片
    [imagePicker setAllowsEditing:YES];
    //是否显示默认相机UI
    [imagePicker setShowsCameraControls:YES];
    [imagePicker setCameraDevice:UIImagePickerControllerCameraDeviceFront];
    [imagePicker setCameraFlashMode:UIImagePickerControllerCameraFlashModeAuto];
    //设置拍摄模式 (拍摄照片或者视频)
    [imagePicker setCameraCaptureMode:UIImagePickerControllerCameraCaptureModePhoto];
    [[UIApplication sharedApplication].keyWindow.rootViewController  presentViewController: imagePicker animated: YES completion:nil];
}
-(void)showPhoto{
  
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self; //设置代理
    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //图片来源
    //相册
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [[UIApplication sharedApplication].keyWindow.rootViewController  presentViewController:imagePickerController animated:YES completion:nil];
    
}

#pragma makr -- 上传图片到服务器
//上传图片
- (void)upDateHeadIcon:(UIImage *)photo
{
   
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值