Reputation: 3445
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?
Upvotes: 7
Views: 5292
Reputation: 836
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)
Upvotes: 9
Reputation: 3445
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];
Upvotes: 17
Reputation: 462
You have your image. Why do addToFile: -> imageNamed: to get your image? Just do:
NSURL *yourURL = [NSURL URLWithString:@"www.somepage.com/yourimage.png"];
NSData *data = [NSData dataWithContentsOfURL:yourURL];
UIImage *yourimage = [UIImage imageWithData:data];
Upvotes: 0