Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGImageDestinationCreateWithData not creating a CGImageDestinationRef object

I'm sure this is something simple, but I just can't find it.

I am trying to create a CGImageDestinationRef using the CGImageDestinationCreateWithData function, but its returning me a nil.

Function documentation:

http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGImageDestination/Reference/reference.html#//apple_ref/c/func/CGImageDestinationCreateWithData

The function is pretty simple but I can't seem to get it to return me an object. I'm in an ARC class so there is a bridging wrapper around the function.

My code snippet:

NSMutableData *data = [[NSMutableData alloc] init];
CGImageDestinationRef imageDestination = nil;

imageDestination = (__bridge CGImageDestinationRef) CFBridgingRelease(CGImageDestinationCreateWithData ((__bridge CFMutableDataRef)data, (__bridge CFStringRef)type, 1, NULL));

if (!imageDestination)
    NSLog(@"This is always called. :-(");

I would greatly appreciate any help from someone who spots me doing something stupid!

like image 200
Damien Avatar asked May 20 '26 17:05

Damien


1 Answers

I've solved it and am posting the answer here as it may help someone else in the future.

I have a type set to be @"image/jpg", which did not seem to work (documentation suggest it should).

When I changed the type to @"public.jpeg", the function started to work as expected.

Using Jim's hint in the comments below, this is what I ended up doing to find the correct UTI type from a URL:

NSString *uti = CFBridgingRelease(UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef)[NSString stringWithFormat:@"image/%@", url.pathExtension], kUTTypeImage));

like image 86
Damien Avatar answered May 22 '26 06:05

Damien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!