超轻量级的 iOS 全景图组件

https://github.com/bestswifter/BSPanoramaView

OpenGL !

Usage


#import "BSPanoramaView.h"

- (void)viewDidLoad {
    [super viewDidLoad];

    BSPanoramaView *panoView = [[BSPanoramaView alloc] initWithFrame:self.view.bounds imageName:@"test"];
    [self.view addSubview:panoView];
}

- (void)setImageWithName:(NSString *)imageName {
    self.shouldUnload = NO;
    /// 将图片转换成为纹理信息,由于OpenGL的默认坐标系设置在左下角, 而GLKit在左上角, 因此需要转换
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], GLKTextureLoaderOriginBottomLeft, nil];
    UIImage *textureImage = [UIImage imageNamed:imageName];
    GLKTextureLoader *loader = [[GLKTextureLoader alloc] initWithSharegroup:self.context.sharegroup];
    [loader textureWithCGImage:textureImage.CGImage options:options queue:dispatch_get_main_queue() completionHandler:^(GLKTextureInfo * _Nullable textureInfo, NSError * _Nullable outError) {
        
        if (self.shouldUnload) {  // 因为是异步加载,所以需要考虑调用完 unloadImage 才加载成功的情况
            [[PanoramaManager sharedInstance] unRegisterView:self];
            GLuint name = textureInfo.name;
            glDeleteTextures(1, &name);
            glDrawElements(GL_TRIANGLES, self.numIndices, GL_UNSIGNED_SHORT, 0);
            return ;
        }
        
        /// 设置着色器的纹理
        self.effect = [[GLKBaseEffect alloc] init];
        self.effect.texture2d0.enabled = GL_TRUE;
        self.effect.texture2d0.name = textureInfo.name;
    }];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容