简体   繁体   English

如何使用GLKTextureLoader从Web URL加载立方体贴图

[英]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 不确定如何使用从Web加载的图像作为GLKit天空盒中的资产(例如旧的苹果/谷歌地图街景视图)。使用GLKTextureLoader加载立方体贴图有两种方法: cubeMapWithContentsOfFilecubeMapWithContentsOfUrl

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? 那么有没有办法从网上加载的图像中获取路径并在此处使用呢?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM