I'm trying to stream images to GPU, and using SKSpriteNode. The images are not in my bundle, and I was wondering if there is some way to work with UIImage instead of imageNamed:
My current idea is
URL -> NSData -> UIImage -> addToFile: -> imageNamed:
Can I add to path and call the image that way, or what would be the best way (is it possible) to reference UIImage by name dynamically when it is not included in bundle?
So, apparantly SKTexture can accomodate UIImage, and I ended up something along the lines of
NSString* urlString = @"http://superawesomeimage.com.jpg";
NSData* imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
UIImage* theImage = [UIImage imageWithData:imageData];
SKView* theView = [[SKView alloc] initWithFrame:self.view.frame];
[self.view addSubview:theView];
SKScene* theScene = [SKScene sceneWithSize:theView.frame.size];
SKSpriteNode* theSprite = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImage:theImage]];
[theScene addChild:theSprite];
Swift Answer
let url = NSURL(string: imageURL)
let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
let theImage = UIImage(data: data!)
let Texture = SKTexture(image: theImage!)
let mySprite = SKSpriteNode(texture: Texture)
self.addChild(mySprite)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With