美文网首页
超轻量级的 iOS 全景图组件

超轻量级的 iOS 全景图组件

作者: 曾柏超 | 来源:发表于2017-11-16 11:56 被阅读0次

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

相关文章

网友评论

      本文标题:超轻量级的 iOS 全景图组件

      本文链接:https://www.haomeiwen.com/subject/jzllvxtx.html