我試圖流圖像到GPU,並使用SKSpriteNode。這些圖像是不是在我的包,我想知道是否有某種方式與UIImage的,而不是imageNamed工作:SKSpriteNode與URL從UIImage
我現在的想法是
URL - >的NSData - >的UIImage - > addToFile: - > imagenamed:
我可以添加到路徑並以這種方式調用圖像,或者什麼是最好的方式(是否有可能)動態引用UIImage時,它不包含在捆綁中?
我試圖流圖像到GPU,並使用SKSpriteNode。這些圖像是不是在我的包,我想知道是否有某種方式與UIImage的,而不是imageNamed工作:SKSpriteNode與URL從UIImage
我現在的想法是
URL - >的NSData - >的UIImage - > addToFile: - > imagenamed:
我可以添加到路徑並以這種方式調用圖像,或者什麼是最好的方式(是否有可能)動態引用UIImage時,它不包含在捆綁中?
所以,apparantly SKTexture可容納的UIImage,我結束了沿
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];
你有你的形象。爲什麼要addToFile: - > imageNamed:獲取圖像?只要做到:
NSURL *yourURL = [NSURL URLWithString:@"www.somepage.com/yourimage.png"];
NSData *data = [NSData dataWithContentsOfURL:yourURL];
UIImage *yourimage = [UIImage imageWithData:data];
我試圖引用圖像,就好像它在捆綁。謝謝! – aug2uag
斯威夫特答東西線
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)
嘗試:URL - >的NSData - >將文件保存到文檔 - > imageNamed: - 因爲您真的不希望每次啓動應用程序或加載場景時都這樣做,您**有**來緩存圖像,因爲有很多人使用帶寬有限的計劃(更不用說慢速連接)。 – LearnCocos2D