VansFannel
VansFannel

Reputation: 45921

UIImage imageWithData returns nil

I'm developing an iOS 5.0+ app with latest SDK.

On my app I have some C++ image recognition software, and I'm trying to save image recognized by this C++ functions.

I have this array to store image recognized:

int _detectedImages[NSMaxNumDetections ][NSPatchSize * NSPatchSize];

I call the C++ function this way:

numberOfDetections = nativeDetect(_resultImages);

And with the following code, I try to convert data from _detectedImages to UIImage.

for (int index = 0; index < numberOfDetections; index++)
{
    if (_resultImages[index] != nil)
    {
        NSData* imageData = [NSData dataWithBytes:&_resultImages[index] length:sizeof(_resultImages[index])];
        NSLog(@"##### Image data size: %d", [imageData length]);
        UIImage* newImage = [UIImage imageWithData:imageData];
        _lastDetectedSignImages[index] = newImage;
    }
}

On the log I get this:

#### Image data size: 2304
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM setObject:atIndex:]: object cannot be nil'
*** First throw call stack:

newImage is nil but imageData has data.

Am I doing something wrong when I try to get an UIImage from that data?

The other possibility if that my C++ recognition software is doing something wrong, but I'm not going to show you that code here (sorry, it's copyrighted).

Or I'm not passing _resultImages correctly to nativeDetect.

Upvotes: 0

Views: 2786

Answers (1)

David Hoerl
David Hoerl

Reputation: 41642

Almost for sure your C++ images are in some bitmap format - RGBA etc. So your first task is to figure out what that format is (and the byte order too!). With that information you can use CGImageCreate() to create a CGImageRef. With that you can create a UIImage.

Upvotes: 1

Related Questions