超輕量級的 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;
    /// 將圖片轉(zhuǎn)換成為紋理信息,由于OpenGL的默認(rèn)坐標(biāo)系設(shè)置在左下角, 而GLKit在左上角, 因此需要轉(zhuǎn)換
    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) {  // 因?yàn)槭钱惒郊虞d,所以需要考慮調(diào)用完 unloadImage 才加載成功的情況
            [[PanoramaManager sharedInstance] unRegisterView:self];
            GLuint name = textureInfo.name;
            glDeleteTextures(1, &name);
            glDrawElements(GL_TRIANGLES, self.numIndices, GL_UNSIGNED_SHORT, 0);
            return ;
        }
        
        /// 設(shè)置著色器的紋理
        self.effect = [[GLKBaseEffect alloc] init];
        self.effect.texture2d0.enabled = GL_TRUE;
        self.effect.texture2d0.name = textureInfo.name;
    }];
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容