Search code examples
iphoneiosuikitcore-graphicsquartz-2d

UIImage initWithData how to wait until complete


When I load an BMP image this way:

NSData * pImageData = [[NSData alloc] initWithBytesNoCopy:pBuffer length:dwSize freeWhenDone:NO];
UIImage * pLoaded = [[UIImage alloc] initWithData:pImageData];
[pImageData release];

and try to display that image with Quartz2D:

CGImageRef cgImage = [pLoaded CGImage];
CGContextSaveGState(context);
CGContextTranslateCTM(context, x, y*2 + h);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(x, y, w, h), cgImage);
CGContextRestoreGState(context);

The image displays as black rectangle or sometimes it is displayed but not fully. When I put sleep for 1 second after initWithData, image is displayed correctly.

Is there an UIKit/Quartz2D API method to wait for image initialization complete?


Solution

  • Everything works when I use:

    NSData * pImageData = [[NSData alloc] initWithBytes:pBuffer length:dwSize];
    

    instead of:

    NSData * pImageData = [[NSData alloc] initWithBytesNoCopy:pBuffer length:dwSize freeWhenDone:NO];
    

    Thanks goes to phix23.