Hello,
I am using an Obj-C image picker to select a picture that I then want to use with oF code. The way I tried doing it is by creating an ofTexture, allocating it to 320x480 GL_RGBA and passing that to the
void iPhoneLoadImageFromUIImage(UIImage *image, GLuint *spriteTexture);
function. The first parameter is just the UIImage I’m getting from my image picker controller/view/whatever class.
This is unfortunately not working.
If I just wait for the image picker to finish then grab the UIImage, the application crashes at line 54 “cgImage = uiImage.CGImage;” of ofxiPhoneUtils.mm.
If I assign this selected UIImage as the image in a UIImageView that is part of my image picker controller/view object, then the app does not crash, but the ofTexture gets filled with white pixels.
Here’s some of my code:
ImagePickerController.h
#import <UIKit/UIKit.h>
#import "ofMain.h"
@interface ImagePickerController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
IBOutlet UIButton *cameraButton;
IBOutlet UIButton *albumButton;
IBOutlet UIImageView *imageView;
BOOL done;
UIImage* image;
}
@property (nonatomic, retain) UIImage* image;
- (IBAction)openCameraPicker;
- (IBAction)openAlbumPicker;
- (bool)isDone;
@end
ImagePickerController.mm
#import "ImagePickerController.h"
@implementation ImagePickerController
@synthesize image;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
done = NO;
}
return self;
}
- (void)viewDidLoad {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// disable the camera button
[cameraButton setEnabled:NO];
[cameraButton setAlpha:0.5];
}
[super viewDidLoad];
}
- (IBAction)openCameraPicker {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker;
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.allowsImageEditing = YES;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
}
}
- (IBAction)openAlbumPicker {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker;
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsImageEditing = YES;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
}
}
- (void)imagePickerController:(UIImagePickerController*)picker
didFinishPickingImage:(UIImage*)img
editingInfo:(NSDictionary*)editingInfo {
[self setImage:img];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker {
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (void)setImage:(UIImage*)img {
image = img;
imageView.image = image; // IF I COMMENT THIS OUT, THE APP CRASHES
done = YES;
}
- (bool)isDone {
return (done == YES);
}
@end
testApp.mm
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
ofBackground(0, 0, 0); // set background to black
ofSetBackgroundAuto(true); // set background to clear automatically every frame
ofSetFrameRate(60); // set desired framerate to 60fps
// initialize the accelerometer and multitouch:
ofxAccelerometer.setup();
ofxMultiTouch.addListener(this);
state = STATE_PICK_IMAGE;
imagePickerController = [[ImagePickerController alloc] initWithNibName:@"ImagePickerView" bundle:nil];
[[[UIApplication sharedApplication] keyWindow] addSubview:imagePickerController.view];
}
//--------------------------------------------------------------
void testApp::update() {
if (state == STATE_PICK_IMAGE && [imagePickerController isDone]) {
printf("PICK IMAGE DONE!\n");
state = STATE_LEFT_EYE;
printf("siiize %f x %f\n", imagePickerController.image.size.width, imagePickerController.image.size.height);
texIn.allocate(imagePickerController.image.size.width, imagePickerController.image.size.height, GL_RGBA);
iPhoneLoadImageFromUIImage([imagePickerController image], &texIn.texData.textureName[0]);
[[imagePickerController view] setAlpha:0];
}
// printf("update()\n");
}
//--------------------------------------------------------------
void testApp::draw() {
if (state == STATE_LEFT_EYE) {
//printf("draw\n");
texIn.draw(0, 0);
}
}
Any ideas?
Thanks,
-Elie