Sprite 套件和 colorWithPatternImage

编程入门 行业动态 更新时间:2024-10-15 04:25:11
本文介绍了Sprite 套件和 colorWithPatternImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们有没有办法在一个区域内重复图像,比如 SKSpriteNode?SKColor colorWithPatternImage 不幸的是不起作用.

Do we have any way of repeating an image across an area, like a SKSpriteNode? SKColor colorWithPatternImage doesn't work unfortunately.

我做了以下分类,到目前为止似乎有效.使用 Mac,未在 iOS 上测试.可能需要对 iOS 进行一些修复.

I did the following categories, it seems to work so far. Using Mac, not tested on iOS. Likely needs some fixing for iOS.

// Add to SKSpriteNode category or something. +(SKSpriteNode*)patternWithImage:(NSImage*)image size:(const CGSize)SIZE; // Add to SKTexture category or something. +(SKTexture*)patternWithSize:(const CGSize)SIZE image:(NSImage*)image;

以及实现.放入各自的文件.

And the implementations. Put in respective files.

+(SKSpriteNode*)patternWithImage:(NSImage*)imagePattern size:(const CGSize)SIZE { SKTexture* texturePattern = [SKTexture patternWithSize:SIZE image:imagePattern]; SKSpriteNode* sprite = [SKSpriteNode spriteNodeWithTexture:texturePattern]; return sprite; } +(SKTexture*)patternWithSize:(const CGSize)SIZE image:(NSImage*)image { // Hopefully this function would be platform independent one day. SKColor* colorPattern = [SKColor colorWithPatternImage:image]; // Correct way to find scale? DLog(@"backingScaleFactor: %f", [[NSScreen mainScreen] backingScaleFactor]); const CGFloat SCALE = [[NSScreen mainScreen] backingScaleFactor]; const size_t WIDTH_PIXELS = SIZE.width * SCALE; const size_t HEIGHT_PIXELS = SIZE.height * SCALE; CGContextRef cgcontextref = MyCreateBitmapContext(WIDTH_PIXELS, HEIGHT_PIXELS); NSAssert(cgcontextref != NULL, @"Failed creating context!"); // CGBitmapContextCreate( // NULL, // let the OS handle the memory // WIDTH_PIXELS, // HEIGHT_PIXELS, CALayer* layer = CALayer.layer; layer.frame = CGRectMake(0, 0, SIZE.width, SIZE.height); layer.backgroundColor = colorPattern.CGColor; [layer renderInContext:cgcontextref]; CGImageRef imageref = CGBitmapContextCreateImage(cgcontextref); SKTexture* texture1 = [SKTexture textureWithCGImage:imageref]; DLog(@"size of pattern texture: %@", NSStringFromSize(texture1.size)); CGImageRelease(imageref); CGContextRelease(cgcontextref); return texture1; }

好的,这也是需要的.这可能仅适用于 Mac.

Ok this is needed as well. This likely only works on Mac.

CGContextRef MyCreateBitmapContext(const size_t pixelsWide, const size_t pixelsHigh) { CGContextRef context = NULL; CGColorSpaceRef colorSpace; void * bitmapData; //int bitmapByteCount; size_t bitmapBytesPerRow; bitmapBytesPerRow = (pixelsWide * 4);// 1 //bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);// 2 bitmapData = NULL; #define kBitmapInfo kCGImageAlphaPremultipliedLast //#define kBitmapInfo kCGImageAlphaPremultipliedFirst //#define kBitmapInfo kCGImageAlphaNoneSkipFirst // According to stackoverflow/a/18921840/129202 it should be safe to just cast CGBitmapInfo bitmapinfo = (CGBitmapInfo)kBitmapInfo; //kCGImageAlphaNoneSkipFirst; //0; //kCGBitmapAlphaInfoMask; //kCGImageAlphaNone; //kCGImageAlphaNoneSkipFirst; context = CGBitmapContextCreate (bitmapData,// 4 pixelsWide, pixelsHigh, 8, // bits per component bitmapBytesPerRow, colorSpace, bitmapinfo ); if (context== NULL) { free (bitmapData);// 5 fprintf (stderr, "Context not created!"); return NULL; } CGColorSpaceRelease( colorSpace );// 6 return context;// 7 }

推荐答案

iOS工作代码:

CGRect textureSize = CGRectMake(0, 0, 488, 650); CGImageRef backgroundCGImage = [UIImage imageNamed:@"background.png"].CGImage; UIGraphicsBeginImageContext(self.level.worldSize); // use WithOptions to set scale for retina display CGContextRef context = UIGraphicsGetCurrentContext(); CGContextDrawTiledImage(context, textureSize, backgroundCGImage); UIImage *tiledBackground = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); SKTexture *backgroundTexture = [SKTexture textureWithCGImage:tiledBackground.CGImage]; SKSpriteNode *backgroundNode = [SKSpriteNode spriteNodeWithTexture:backgroundTexture]; [self addChild:backgroundNode];

更多推荐

Sprite 套件和 colorWithPatternImage

本文发布于:2023-11-17 02:48:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608535.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:套件   Sprite   colorWithPatternImage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!