Search code examples
glkitpanoramasskyboxios6.0glktextureloader

How to use GLKTextureLoader to load a cubemap from a web url


Unsure how to use an image loaded from the web as an asset in a GLKit skybox (like the old apple/google maps streetview) There are 2 methods for loading cubemaps with GLKTextureLoader: cubeMapWithContentsOfFile and cubeMapWithContentsOfUrl

If I grab the image locally it works fine:

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

So is there a way to get a path from an image loaded from the web and use it here?


Solution

  • I ended up loading the image from the web like so:

    - (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];
        }
    }
    

    caching and returning the file url via :

    - (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;
    }
    

    and loading the file via

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