Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SKSpriteNode with UIImage from URL

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?

like image 861
aug2uag Avatar asked Oct 09 '13 19:10

aug2uag


2 Answers

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];
like image 102
aug2uag Avatar answered Dec 04 '22 04:12

aug2uag


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)
like image 36
brilliantairic Avatar answered Dec 04 '22 05:12

brilliantairic