如何将UIImage(从照片库中挑选)渲染到CIContext / GLKView中?

时间:2013-10-21 13:49:16

标签: iphone xcode uiimage core-image glkview

经过研究和尝试很多事情,我终于问了一下:

基本上我想选择一张照片,然后将其渲染为CIcontext ,因为他们知道许多其他图像渲染技术可用(例如使用UIImageView等等),我必须使用低级OpenGL ES渲染。

请检查我的完整代码(UIImagePickerControllerAppDelegate的预加载除外)

#import "ViewController.h"
#import "AppDelegate.h"
#import <GLKit/GLKit.h>

@interface ViewController () <GLKViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
    GLKView *glkview;
    CGRect glkview_bounds;
}

- (IBAction)click:(id)sender;
@property (strong, nonatomic) EAGLContext *eaglcontext;
@property (strong, nonatomic) CIContext *cicontext;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.eaglcontext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    glkview = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds] context:self.eaglcontext];
    glkview.drawableDepthFormat = GLKViewDrawableDepthFormat24;
    glkview.enableSetNeedsDisplay = YES;
    glkview.delegate = self;
    [self.view addSubview:glkview];
    [glkview bindDrawable];

    glkview_bounds = CGRectZero;
    glkview_bounds.size.width = glkview.drawableWidth;
    glkview_bounds.size.height = glkview.drawableHeight;
    NSLog(@"glkview_bounds:%@", NSStringFromCGRect(glkview_bounds));

    self.cicontext = [CIContext contextWithEAGLContext:self.eaglcontext options:@{kCIContextWorkingColorSpace : [NSNull null]} ];
    UIAppDelegate.g_mediaUI.delegate = self;
}

- (IBAction)click:(id)sender {
    [self presentViewController:UIAppDelegate.g_mediaUI animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [self dismissViewControllerAnimated:NO completion:nil];

    UIImage *pre_img = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage];
    CIImage *outputImage = [CIImage imageWithCGImage:pre_img.CGImage];

    NSLog(@"orient:%d, size:%@, scale:%f, extent:%@", (int)pre_img.imageOrientation, NSStringFromCGSize(pre_img.size), pre_img.scale,NSStringFromCGRect(outputImage.extent));

    if (outputImage) {
        if (self.eaglcontext != [EAGLContext currentContext])
            [EAGLContext setCurrentContext:self.eaglcontext];
        // clear eagl view to grey
        glClearColor(0.5, 0.5, 0.5, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
        // set the blend mode to "source over" so that CI will use that
        glEnable(GL_BLEND);
        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
        [self.cicontext drawImage:outputImage inRect:glkview_bounds fromRect:[outputImage extent]];
        [glkview display];
    }
}

@end

什么有效:挑选的图片大小如下。显示尺寸为1390x1390或1440x1440。

什么不起作用:不会显示尺寸为2592x1936的拾取图像,基本上都是用相机拍摄的所有大图片。

请帮助找出解决方案,我被困在这里......

1 个答案:

答案 0 :(得分:1)

OpenGL具有纹理限制,具体取决于iOS设备,它可以是2048x2048。 您可以使用以下代码检查设备的最大纹理限制:

static GLint maxTextureSize = 0;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);

较旧的设备(iPhone 4S之前)的最大纹理尺寸均为2048x2048。 A5及以上(包括iPhone 4S之后)的纹理尺寸最大为4096x4096。

来源:Black texture with OpenGL when image is too big