Question

I'm trying to move some image loading into the background. Currently i'm loading UIImages in the background but from what I have read, this is not the suggested way to go about doing it, instead I should load the CGImageRef in the background, then load the UIImage from it in the main thread.

The problem is that when I try to create a CGImageRef, its coming back as null. Sample code:

NSData * imageData = [NSData dataWithContentsOfFile: coverPath];
if(nil != imageData)
{
    UIImage * uiImage = [UIImage imageWithData: imageData];

    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef) imageData);
    CGImageRef imageRef = CGImageCreateWithPNGDataProvider(provider, NULL, true, kCGRenderingIntentDefault);

    NSLog(@"Test: %p, %p", imageRef, uiImage);
    CGDataProviderRelease(provider);
}

Which logs out Test: 0x0, 0x1c566bb0. Meaning the imageRef is null but the uiImage is not. Any ideas what i'm doing wrong here? It seems as if this should be quite simple?

Était-ce utile?

La solution

CGImageCreateWithPNGDataProvider()

returns nil if the provided data is not in PNG format (for example JPEG or TIFF).

[UIImage imageWithData: imageData]

returns an image for all supported image file formats (PNG, JPEG, TIFF etc.)

This explains why the first function can fail while the second succeeds.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top