3

After researching and trying many things finally I made myself to ask SO:

basically I would like to pick a photo and then render it to a CIcontext, knowing that many other image rendering techniques available (eg. use UIImageView, etc), I have to use low-level OpenGL ES rendering.

Please check my full code (except the preload of UIImagePickerController in AppDelegate)

#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

What works: picked images having sizes eg. size 1390x1390 or 1440x1440 are displayed.

What doesn't work: picked images with size of 2592x1936 are not displayed, basically all large pictures which taken with the camera.

Please help finding out the solution, I'm stucked here...

Arun_
  • 1,806
  • 2
  • 20
  • 40
Zsolt Normann
  • 167
  • 2
  • 5
  • sadly no.. I tried sooo many variations. I also saw a WWDC session on this topic but its source code is not released (maybe yet).. it is wwdc2012/511: Core Image Techniques. I've checked the source code dmg, but it was not containing session 511 source code... – Zsolt Normann Oct 30 '13 at 10:15
  • this solution not working on ios 10.2 – khunshan Feb 06 '17 at 15:12

1 Answers1

1

OpenGL has a texture limit, depending on the iOS device it can be 2048x2048. You can check the max texture limit for the device using the following code:

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

Older devices (before iPhone 4S) all have a maximum texture size of 2048x2048. A5 and above (after and including iPhone 4S) have a maximum 4096x4096 texture size.

Source: Black texture with OpenGL when image is too big

Community
  • 1
  • 1
gabriellanata
  • 3,946
  • 2
  • 21
  • 27
  • Tested on my iPhone4 and it logged 2048. So it means practically that before I render to my cicontext, I have to shrink it with eg. LanczosFilter (CILanczosScaleTransform)? – nzs Oct 31 '13 at 18:40
  • Yes, shrink it to a size thats inside the allowed limit. You could also slice it into pieces and draw them separately, but thats probably more touble than its worth. – gabriellanata Oct 31 '13 at 21:53
  • In nearly all cases te OpenGL texture size limit is bigger than the screen of the device. – gabriellanata Oct 31 '13 at 21:54
  • Tested with various sizes and yes, the problem is the texture size limit. Thanks for this! – nzs Nov 01 '13 at 11:40