2013-08-21 19 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]; 
加载文件