【问题标题】:Add UIImage to CCSprite? [closed]将 UIImage 添加到 CCSprite? [关闭]
【发布时间】:2011-11-11 05:15:22
【问题描述】:

我不知道这是否愚蠢,但我已经为此挠头大约一两个小时了。

我从NSUserDefaults 获取NSData 对象,然后将其转换为UIImage。现在如何将我的CCSprite 的图像设置为我的 UIImage?

spriteWithFile 在这里不起作用,因为它需要NSString。我正在寻找具有UIImage 参数的API。这可能吗?

有什么想法吗?

Edit1:这是怎么回事?

UIImage *image = [UIImage imageWithData:(NSData*)obj];
sprite = [CCSprite spriteWithCGImage:image.CGImage key:@"mainSprite"];

【问题讨论】:

    标签: ios image api cocos2d-iphone


    【解决方案1】:

    试试这个:

    CCTexture2D *tex = [[[CCTexture2D alloc] initWithImage:uiImage] autorelease];
    CCSprite *sprite = [CCSprite spriteWithTexture:tex];
    

    uiImage 是一个 UIImage 对象。

    【讨论】:

    • 我必须使用 CCSprite :/ 在第一个示例中,您使用的是使用 NSString 的正常方式。我需要传入 UIImage 而不是 NSString!
    • 我编辑的代码在上面,请检查一下。
    • initWithImage 已弃用。您介意展示应该使用的较新 API 的使用吗?除了图片,精灵的物理外观并没有什么不同,因为我使用的是 CCTexture2D 对吧?
    • 在我的原始帖子中查看edit1
    • initWithImage: 当前版本的cocos2d中不存在方法
    【解决方案2】:

    这种方法对我来说非常有效......

     -(CCSprite*)spriteFromDiskFileName:(NSString*)fileName
        {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,   NSUserDomainMask, YES);
            NSString *directoryPath = [paths objectAtIndex:0] ;
            NSData *imageData = [[NSData alloc]initWithContentsOfFile:[directoryPath stringByAppendingPathComponent:fileName]];
    
            UIImage *spriteImage = [UIImage imageWithData:imageData];
    
            if (spriteImage) {
                printf("\nUIImage created from disk image \n");
            }
            else
            {
                printf("\nUIImage creation failed from disk image \n");
            }
    
            CCTexture2D *texture = [[CCTexture2D alloc] initWithCGImage:spriteImage.CGImage resolutionType:kCCResolutionUnknown];
            return [CCSprite spriteWithTexture:texture];
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多