0

不确定如何使用从网络加载的图像作为 GLKit 天空盒中的资产(如旧的苹果/谷歌地图街景) 使用 GLKTextureLoader 加载立方体贴图有两种方法:cubeMapWithContentsOfFilecubeMapWithContentsOfUrl

如果我在本地抓取图像,它可以正常工作:

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

那么有没有办法从从网络加载的图像中获取路径并在这里使用它?

4

1 回答 1

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

通过以下方式缓存并返回文件 url:

- (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];
于 2013-08-21T02:58:29.773 回答