2013-08-21 35 views
0

不確定如何在GLKit天空盒使用從網上下載的圖像作爲一種資產(如舊的蘋果/谷歌地圖街景)有兩種方法用於加載立方體貼圖與GLKTextureLoader:cubeMapWithContentsOfFilecubeMapWithContentsOfUrl如何使用GLKTextureLoader從Web URL加載一個立方體貼圖

如果我抓住本地的圖像,它工作正常:

NSString *path = [[NSBundle mainBundle] pathForResource:@"pano" ofType:@"jpg"]; 
GLKTextureInfo *skyboxCubemap = [GLKTextureLoader cubeMapWithContentsOfFile:imgPath options:options error:&error]; 

那麼,有沒有一種方式來獲得從網站加載的圖像的路徑,並在這裏使用它?

回答

0

我結束了從網上像這樣加載圖像:

- (void) cacheImage: (NSURL *) ImageURL : (NSString *)imageName 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *dir = [paths objectAtIndex: 0]; 
    NSString *file = [docDir stringByAppendingPathComponent: imageName]; 

    if(![[NSFileManager defaultManager] fileExistsAtPath: file]) 
    { 
     NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL]; 
     UIImage *image = [[UIImage alloc] initWithData: data]; 
     [UIImageJPEGRepresentation(image, 100) writeToFile: docFile atomically: YES]; 
    } 
} 

緩存,並通過返回文件網址:

- (NSString *) getCachedImage : (NSString *)imageName 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains 
    (NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString* cachedPath = [documentsDirectory stringByAppendingPathComponent:imageName]; 
    NSString *path; 
    if([[NSFileManager defaultManager] fileExistsAtPath: cachedPath]) 
    { 
     path = cachedPath; 
    } 
    return path; 
} 

,並通過

NSString *cached = [self getCachedImage:@"cacheKey"]; 
self.skyboxCubemap = [GLKTextureLoader cubeMapWithContentsOfFile:cached options:options error:&error]; 
加載文件