3

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!

1 Answer 1

3

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));

4
  • 2
    If you have a MIME type such as image/jpeg then you can use the UTI API (import <MobileCoreServices/MobileCoreServices.h>) to sort it out: NSString * uti = CFBridgingRelease(UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef)mimeType, NULL);. The last parameter can narrow down the match too: pass kUTTypeImage to say you expect an image type, for example.
    – Jim Dovey
    Jul 6, 2012 at 14:23
  • Thanks Jim. Handy function there.
    – Damien
    Jul 6, 2012 at 14:45
  • Hehe, didn't recognize you there :o) Look in EPUBProtocol.m or AQHTTPResponseOperation.m and you'll see us using that to get a MIME type from a file extension. Very nice & useful API.
    – Jim Dovey
    Jul 7, 2012 at 17:17
  • Instead of manually constructing the MIME type (and assuming the file extension will be a valid MIME type component), you could use CoreServices to do the path extension to UTI conversion directly: UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)url.pathExtension, NULL) Jul 13, 2015 at 2:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.