How to convert ofImage to UIImage?

OF provides good helper methods to convert UIImage to ofImage and of Texture and so on but I need to do the opposite, I have an ofImage that I need to convert as UIImage, how can I do that?

Thanks

convert it to an iplimage and then make it a UIImage.

IplImage, OpenCV? can you provide an example please? there most be another way using the pixel array data, but I cant figure it out

sounds like we need ofxiPhoneOFImageToUIImage

I created an issue here:
https://github.com/openframeworks/openFrameworks/issues/1735

This seems to provide some ref code for doing the uiimage creation:
http://iphonedevsdk.com/forum/iphone-sdk-development/23525-cgimagecreate-alpha.html

also some more here:
http://forum.openframeworks.cc/t/ofimageoftexture–gt;-uiimage/7443/0

Thanks a lot Theo I got it working from the second link, here is the code:

Variable declaration in testApp.h

  
ofxiPhoneImagePicker photoAlbum;  
ofImage myImage;  

Code snippet from my View Controller class

  
int width = ofApp->myImage.width;  
int height = ofApp->myImage.height;  
int nrOfColorComponents = 4; //RGBA  
int bitsPerColorComponent = 8;  
int rawImageDataLength = width * height * nrOfColorComponents;  
BOOL interpolateAndSmoothPixels = NO;  
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;  
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;  
	  
CGDataProviderRef dataProviderRef;  
CGColorSpaceRef colorSpaceRef;  
CGImageRef imageRef;  
  
GLubyte *rawImageDataBuffer = ofApp->photoAlbum.pixels;  
		  
dataProviderRef = CGDataProviderCreateWithData(NULL, rawImageDataBuffer, rawImageDataLength, nil);  
colorSpaceRef = CGColorSpaceCreateDeviceRGB();  
imageRef = CGImageCreate(width, height, bitsPerColorComponent, bitsPerColorComponent * nrOfColorComponents, width * nrOfColorComponents, colorSpaceRef, bitmapInfo, dataProviderRef, NULL, interpolateAndSmoothPixels, renderingIntent);  
myImage = [UIImage imageWithCGImage:imageRef];  

thanks for posting your solution nardove.
Could you also reveal what the some of the variables refer to?
I assume
ofApp->myImage.width refers to the width of the image we want to transform (ofApp->myImage.height would be it’s height)

I was esspecially wondering about
ofApp->photoAlbum.pixels ? what is photoAlbum in your case? (this is where my code doesn’t work as I don’t know what to put there

last line’s myImage I thought would be the UIImage, am I right?
(maybe you could post your variable declarations?)
thanks in advance for helping me out!

Sure, I just updated my previous post to add my variable declarations.

Basically all I’m doing is getting an image from the photo album like described on the following link
https://gist.github.com/4245137

Long story short, I need to convert the selected image (in this case of type ofImage) to UIImage to update my GUI which image the user has selected.

Hope that helps

thanks again, but I guess now you confused me completely:
why is myImage not an UIImage as we are converting it to one, right?
maybe this makes more sense to you if I copypaste my code here. What I am trying to do is saving an image to my photo library (I found the saving code somewhere)

  
  
ofImage savedImage.cropFrom(imageToCrop, touch1x, touch1y, sizeX, sizeY);  
    UIImage *image;  //saved image needs to be converted  
  
     //converting savedImage into UIImage  
       
     int width = savedImage.width;  
     int height = savedImage.height;  
     int nrOfColorComponents = 4; //RGBA  
     int bitsPerColorComponent = 8;  
     int rawImageDataLength = width * height * nrOfColorComponents;  
     BOOL interpolateAndSmoothPixels = NO;  
     CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;  
     CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;  
       
     CGDataProviderRef dataProviderRef;  
     CGColorSpaceRef colorSpaceRef;  
     CGImageRef imageRef;  
       
     GLubyte *rawImageDataBuffer = savedImagePixels;  
       
     dataProviderRef = CGDataProviderCreateWithData(NULL, rawImageDataBuffer, rawImageDataLength, nil);  
     colorSpaceRef = CGColorSpaceCreateDeviceRGB();  
     imageRef = CGImageCreate(width, height, bitsPerColorComponent, bitsPerColorComponent * nrOfColorComponents, width * nrOfColorComponents, colorSpaceRef, bitmapInfo, dataProviderRef, NULL, interpolateAndSmoothPixels, renderingIntent);  
     image = [UIImage imageWithCGImage:imageRef];  
      
    //this should be saving  
    if (image != nil)  
    {  
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  
                                                             NSUserDomainMask, YES);  
        NSString *documentsDirectory = [paths objectAtIndex:0];  
        NSString* path = [documentsDirectory stringByAppendingPathComponent:  
                          [NSString stringWithString: @"test.png"] ];  
        NSData* data = UIImagePNGRepresentation(image);  
        [data writeToFile:path atomically:YES];  
    }  
  

however right now it’s not saving anything… any ideas?

Sorry suse_m my bad!

There are 2 myImage objects one lives in testApp and the other in myViewController which is the class I use for my nib file so where you see ofApp->myImage I refere to the one in testApp and plain myImage is of type UIImage in my view controller

I should rename my variables better but that was a quick late night hack trying to get it to work

Back to your problem, if you NSLog path and documentsDirectory what do you get?

ok.
according to that my code should work as is (I think) but if I set a breakpoint right after the conversion and before the saving, the UIImage seems to be empty… so something wrong is going on, I think…

Are you using the right ofImage source?

From your code I can see you pass a parameter called imageToCrop which you dont use, instead you work with another variable called savedImage and savedImagePixels, make sure these has been set

That is the only thing odd I can think of, hope it works :wink:

yes it set the image. I can also display just that one if I want.
however, just noticed that the error seems to be somewhere in the saving, although I couldn’t figure out where yet. at least it is actually going into the loop which it wouldn’t if there was no data in my image variable…
so BIG thanks for converting code.
learning so far: I can convert but still no success in storing… :wink:

You may have seen this already but if not, in this post they discuss how to save an image in to the photo album
http://forum.openframeworks.cc/t/ofimageoftexture–gt;-uiimage/7443/0

Hope that helps

yes I did, but honestly I don’t understand a single line of code in there…
thanks for the hint anyways!

here’s a simplified / self contained version of nardove’s code:

    UIImage * UIImageFromOFImage( ofImage & img ){
    int width = img.width;
    int height =img.height;
    
    int nrOfColorComponents = 1;
    
    if (img.type == OF_IMAGE_GRAYSCALE) nrOfColorComponents = 1;
    else if (img.type == OF_IMAGE_COLOR) nrOfColorComponents = 3;
    else if (img.type == OF_IMAGE_COLOR_ALPHA) nrOfColorComponents = 4;
    
    int bitsPerColorComponent = 8;
    int rawImageDataLength = width * height * nrOfColorComponents;
    BOOL interpolateAndSmoothPixels = NO;
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    CGDataProviderRef dataProviderRef;
    CGColorSpaceRef colorSpaceRef;
    CGImageRef imageRef;
    GLubyte *rawImageDataBuffer = img.getPixels();
    dataProviderRef = CGDataProviderCreateWithData(NULL, rawImageDataBuffer, rawImageDataLength, nil);
    colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    imageRef = CGImageCreate(width, height, bitsPerColorComponent, bitsPerColorComponent * nrOfColorComponents, width * nrOfColorComponents, colorSpaceRef, bitmapInfo, dataProviderRef, NULL, interpolateAndSmoothPixels, renderingIntent);
    UIImage * uimg = [UIImage imageWithCGImage:imageRef];
    return uimg;
    
}
1 Like