2013-10-09 2 views
7

이미지를 GPU로 스트리밍하고 SKSpriteNode를 사용하려고합니다. 이미지 내 번들에 포함되지 않으며, 대신 imageNamed의있는 UIImage와 함께 작동 할 수있는 방법이 있는지 궁금 해서요 :URL의 UIImage가있는 SKSpriteNode

나의 현재 생각입니다

URL ->을 NSData ->있는 UIImage -> addToFile : -> imageNamed :

번들에 포함되지 않은 경우 경로를 추가하여 이미지를 그런 식으로 호출하거나 UIImage를 이름으로 동적으로 참조 할 수있는 가장 좋은 방법은 무엇입니까?

+0

시도 : URL ->을 NSData -> 파일을 문서에 저장 -> imageNamed : - 앱이 시작하거나 장면을로드 할 때마다 그렇게하고 싶지 않기 때문에 ** 대역폭 제한 계획에 많은 사람들이 있기 때문에 이미지를 캐시 할 수 있습니다 ** (느린 연결은 말할 것도 없습니다). – LearnCocos2D

답변

17

을 따라서, 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]; 
-1

이미지가 있습니다. 왜 addToFile을합니까? : -> imageNamed : 이미지를 얻으려면? 그냥 수행

NSURL *yourURL = [NSURL URLWithString:@"www.somepage.com/yourimage.png"]; 
NSData *data = [NSData dataWithContentsOfURL:yourURL]; 
UIImage *yourimage = [UIImage imageWithData:data]; 
+0

이미지를 번들처럼 참조하려고합니다. 감사! – aug2uag

5

신속한 응답의 라인을 따라 뭔가를 결국

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)