不确定如何使用从Web加载的图像作为GLKit天空盒中的资源(如旧的apple / google maps streetview)有两种方法可以使用GLKTextureLoader加载立方体贴图: cubeMapWithContentsOfFile 和 cubeMapWithContentsOfUrl
如果我在本地抓取图像,它可以正常工作:
NSString *path = [[NSBundle mainBundle] pathForResource:@"pano" ofType:@"jpg"];
GLKTextureInfo *skyboxCubemap = [GLKTextureLoader cubeMapWithContentsOfFile:imgPath options:options error:&error];
那么有没有办法从网上加载的图像中获取路径并在此处使用它?
答案 0 :(得分: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];