Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a UIImage from another UIImage using imageWithData returns nil

Tags:

iphone

uiimage

I'm trying to retrieve the image data from one uiimage, modify it, and create a new uiimage from it. To start, I tried just copying the data without any modification to get the basics down - but that fails (UIImage +imageWithData: returns nil). Can anyone see why this wouldn't work?

// I've confirmed that the follow line works

UIImage *image = [UIImage imageNamed:@"image_foo.png"];

// Get a reference to the data, appears to work

CFDataRef dataRef = CGDataProviderCopyData(CGImageGetDataProvider([image CGImage]));

// Get the length of memory to allocate, seems to work (e.g. 190000)

int length = CFDataGetLength(dataRef) * sizeof(UInt8);

UInt8 * buff = malloc(length);

// Again, appears to work 

CFDataGetBytes(dataRef, CFRangeMake(0,length),buff);

// Again, appears to work

NSData * newData = [NSData dataWithBytesNoCopy:buff length:length];

// This fails by returning nil

UIImage *image2 = [UIImage imageWithData:newData]; 

Note:

I also tried using:

UInt8* data = CFDataGetBytePtr(dataRef);

and just piping that straight into NSData.

Same result.

like image 282
user375334 Avatar asked Nov 14 '22 08:11

user375334


1 Answers

I believe that imageWithData takes image-file-data, not image-display-data, which is what you're passing it. Try something like this:

NSData * newData = UIImageJPEGRepresentation(image, 1.0);
UIImage *image2 = [UIImage imageWithData:newData]; 

UIImageJPegRepresentation() returns the data that you would write to a file to create a .jpg file on disk. That, I'm 99.44% sure is what imageWithData: wants.

NOTE: if you're looking to manipulate the data before creating image2, then you do want the display data, in which case the way you get an image back from that is a bit more complicated, but looks something like this:

    // Create a color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    if (colorSpace == NULL)
    {
        fprintf(stderr, "Error allocating color space\n");
        return nil;
    }

    CGContextRef context = CGBitmapContextCreate (bits, size.width, size.height,
            8, size.width * 4, colorSpace,
            kCGImageAlphaPremultipliedLast | IMAGE_BYTE_ORDER
            );
    CGColorSpaceRelease(colorSpace );

    if (context == NULL)
    {
        fprintf (stderr, "Error: Context not created!");
        return nil;
    }

    CGImageRef ref = CGBitmapContextCreateImage(context);
    //free(CGBitmapContextGetData(context));                                      //* this appears to free bits -- probably not mine to free!
    CGContextRelease(context);

    UIImage *img = [UIImage imageWithCGImage:ref];
    CFRelease(ref);                                                             //* ?!?! Bug in 3.0 simulator.  Run in 3.1 or higher.

    return img;

(The above code is derived from examples by Erica Sadun. The smart parts are hers; the mistakes are all mine. But this is the general idea, and it should work.)

like image 188
Olie Avatar answered Dec 14 '22 23:12

Olie