开发者社区> 问答> 正文

cocos2d-x中调用ios图库的方法如何设置

问题:1、进入helloworld创建按钮(已完成); 2、点击按钮直接转到图库或者通过另外一个界面和按钮到图库中(代码已经写好); 3、调用里面的图片作为背景(本人认为已经写好,有不妥之处还请修改); 4、返回helloworld界面(应该有了,没有请帮忙添加) 要求:本人用的是cocos2d-x 2.0.1,本人猜测可能是没有设置rootviewcontroller但是又不知道如何设置,请高手指教! demo在:

展开
收起
杨冬芳 2016-07-01 14:01:56 2432 0
1 条回答
写回答
取消 提交回答
  • IT从业

    http://www.cnblogs.com/hanhongmin/p/3498102.html
    我们在PickerDelegate中能够拿到UIImage拿到后转变成cocos2dx可用的格式,回调HelloWord的方法再干一些事,比如显示出来。

    关于把方法当参数传进来之类的我一概不懂,所以在PickerDelegate中直接调HelloWorld,所以.m改成.mm以实现混编。

    我们先说UIImage的数据转换

    复制代码
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        //CCLog("selected");
        //处理数据
        UIImage* image = [info valueForKey:UIImagePickerControllerOriginalImage];
        
        CGImageRef imageRef = [image CGImage];
        NSUInteger width = CGImageGetWidth(imageRef);
        NSUInteger height = CGImageGetHeight(imageRef);
        CCLog("%i,%i",width,height);
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        unsigned char* rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
        NSUInteger bytesPerPixel = 4;
        NSUInteger bytesPerRow = bytesPerPixel * width;
        NSUInteger bitsPerComponent = 8;
        CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        CGColorSpaceRelease(colorSpace);
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
        CGContextRelease(context);
        cocos2d::CCTexture2D *pickedImage = new cocos2d::CCTexture2D();
        pickedImage->initWithData(rawData, cocos2d::kCCTexture2DPixelFormat_RGBA8888, width, height, cocos2d::CCSizeMake(width, height));
        CCLOG("%f,%f",pickedImage->getContentSize().width,pickedImage->getContentSize().height);
        
        [picker dismissModalViewControllerAnimated: YES];
        [picker release];
    }
    复制代码
    以上,我们已经把图片数据转换到了CCTexture2D,至于为何这么转,我也是搜来哒~注意引入响应头文件,这个以后就不说了。
    
    HelloWorld声明和定义接口,接收CCTexture2D并干一些我们想干的事。
    
    .h
    
    void pickedPhoto(CCTexture2D* texture);
    .cpp
    
    void HelloWorld::pickedPhoto(CCTexture2D* texture){
        CCSprite* sprite = CCSprite::createWithTexture(texture);
        this->addChild(sprite);
    }
    CCScene* HelloWorld::scene()
    {
        CCScene *scene = CCScene::create();
        HelloWorld *layer = HelloWorld::create();
        layer->setTag(1);
        scene->addChild(layer);
    
        return scene;
    }

    screenshot
    UIImage+fixOrientation.h文件

    复制代码
    1 //
    2 // UIImage+fixOrientation.h
    3 // OpenCamera
    4 //
    5 // Created by HanHongmin on 13-12-30.
    6 //
    7 //
    8
    9 #import
    10
    11 @interface UIImage (fixOrientation)
    12 -(UIImage*) fixOrientation;
    13 @end
    复制代码
    UIImage+fixOrientation.m文件

    复制代码
    1 //
    2 // UIImage+fixOrientation.m
    3 // OpenCamera
    4 //
    5 // Created by HanHongmin on 13-12-30.
    6 //
    7 //
    8
    9 #import "UIImage+fixOrientation.h"
    10
    11 @implementation UIImage (fixOrientation)
    12 - (UIImage *)fixOrientation {
    13 if (self.imageOrientation == UIImageOrientationUp) return self;
    14
    15 UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    16 [self drawInRect:(CGRect){0, 0, self.size}];
    17 UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
    18 UIGraphicsEndImageContext();
    19 return normalizedImage;
    20 }
    21 @end
    复制代码
    以上有啥不明白的,自己搜去吧我也说不明白。这个东西用了之后,据说手机内存会突然增大大概40-50M,求良药啊

    在PickerDelegate中引用上面的头文件,得到UIImage后调一下。

    UIImage* image = [info valueForKey:UIImagePickerControllerOriginalImage];
    image = [image fixOrientation];

    试试~~

    图片拾取后发现只显示了一部分,

    自行处理一下吧。我在项目中用的时候,sprite是早就创建好的,大小是整个屏幕,只是获得Texture的时候更换到了sprite中,没啥问题的。

    2019-07-17 19:49:59
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载