ofxiOSExtras problem

Hi all,

I am new to openframeworks I have a problem in ofxiOSExtras.
I want to convert uiimage to of image but I am not able to do that I have tried my level best but of no avail.
I am using ofxiOSExtras class and calling the function ofxiOSUIImageToOFImage but the application is crashing in the function at point “outImage.setFromPixels(pixels, width, height, ofImageMode, true);”
Can some one suggest some solution.

Hi,

In order for someone to help you out I suggest that you post more of your code. If you follow the OF pattern in your project and use the setup and update functions you could post those to functions to start with. Are you new to C++ as well?

Hi Wiklund
Thanks for your reply. Actually I have a UIImagePicker for which after picking the image from camera I pass the image to this function to get OFImage from UIImage. But the app is crashing
Here is my code

void ofApp::loadImage(UIImage * myimage)
{
ofImage myOFimage;
ofxiOSUIImageToOFImage(myimage, myOFimage);
}

OK, I haven’t used ofxiOSExtras myself, but there are a couple of things you could check.

  • set a debug breakpoint at the same line as ofxiOSUIImageToOFImage(myimage, myOFimage);
    what is the value of myimage at that point? Does it point at something usefull, is it 0, or is it not initialized at all perhaps?

  • myOFImage will get out of scope when loadImage returns (it won’t exist). If you’re trying to use myOFImage after loadImage is done xcode should warn about this. Or maybe you declared another ofImage myOfImage outside of loadImage? Either way it really makes no sense letting myOFimage die when loadImage is done. Make loadImage return an ofImage instead:


ofImage ofApp::loadImage(UIImage * myimage)
{
ofImage myOFimage;
ofxiOSUIImageToOFImage(myimage, myOFimage);
return myOFimage;
}

//then use it like this
ofImage myImage = loadImage( pointerToUIImage );


Hope this helps!

Hi Wiklund,

Thanks a lot but my actual problem is that my app is crashing in the function " ofxiOSUIImageToOFImage(myimage, myOFimage); "

this function does the following

bool ofxiOSUIImageToOFImage(UIImage *uiImage, ofImage &outImage, int targetWidth, int targetHeight) {
if(uiImage == nil) {
return false;
}

CGContextRef spriteContext;
CGImageRef cgImage = uiImage.CGImage;

int bytesPerPixel = CGImageGetBitsPerPixel(cgImage)/8;
if(bytesPerPixel == 3) bytesPerPixel = 4;

int width = targetWidth > 0 ? targetWidth : CGImageGetWidth(cgImage);
int height = targetHeight > 0 ? targetHeight : CGImageGetHeight(cgImage);

// Allocated memory needed for the bitmap context
GLubyte * pixels = (GLubyte *)malloc(width * height * bytesPerPixel);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

// Uses the bitmatp creation function provided by the Core Graphics framework.
spriteContext = CGBitmapContextCreate(pixels,
                                      width,
                                      height,
                                      CGImageGetBitsPerComponent(cgImage),
                                      width * bytesPerPixel,
                                      colorSpace,
                                      bytesPerPixel == 4 ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);

if(spriteContext == NULL) {
	ofLogError("ofxiOSExtras") << "ofxiOSUIImageToOFImage(): CGBitmapContextCreate returned NULL";
	free(pixels);
	return false;
}

CGContextSetBlendMode(spriteContext, kCGBlendModeCopy);

// After you create the context, you can draw the sprite image to the context.
ofLogVerbose("ofxiOSExtras") << "ofxiOSUIImageToOFImage(): about to CGContextDrawImage";
CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), cgImage);

// You don't need the context at this point, so you need to release it to avoid memory leaks.
ofLogVerbose("ofxiOSExtras") << "ofxiOSUIImageToOFImage(): about to CGContextRelease";
CGContextRelease(spriteContext);

ofImageType ofImageMode;

switch(bytesPerPixel) {
	case 1:
		ofImageMode = OF_IMAGE_GRAYSCALE;
		break;
	case 3: 
		ofImageMode = OF_IMAGE_COLOR;
		break;
	case 4: 
	default:
		ofImageMode = OF_IMAGE_COLOR_ALPHA; break;
}
		
ofLogVerbose("ofxiOSExtras") << "ofxiOSUIImageToOFImage(): about to setFromPixels";
outImage.setFromPixels(pixels, width, height, ofImageMode, true);

free(pixels);

return true;

}

and my app crash at third last line of this function
outImage.setFromPixels(pixels, width, height, ofImageMode, true);

I have checked the parameters to " outImage.setFromPixels " function All parameters have some value.

Can you suggest me any other way of converting UIImage to OFImage.
Thanks

Hi all,

Any suggestions regarding UIImage into OFImage?