nax_
nax_

Reputation: 469

Create UIImage from raw data

I am currently doing image modification. I used a class of my own named "Image".

It looks like this:

@interface Image : NSObject {
    @public
        int height;
        int width;
        float* data;
}

So in data I have floats, in rgba format which mean they go four by four. I can't change my Image class.

I need to create a UIImage from my Image. I'm trying something like that:

+(UIImage *)getUIImageFromImage:(Image *)_img {
    NSData * imageData = (NSData *) _img->data;
    return [[UIImage alloc] initWithData:imageData];
} 

Am I doing something wrong ? Is my cast to (NSData *) okay?

Upvotes: 1

Views: 2617

Answers (1)

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

No it ain't. NSData is an Objective-C object. float * is a raw pointer to one or more float values.

If you tell us where this data comes from and what format it's in, we may be able to suggest a way to convert it to either an NSData object or to a CGImage Quartz value, which can then be wrapped in a UIImage and used with UIKit classes.

Upvotes: 2

Related Questions