Note that there are some explanatory texts on larger screens.

plurals
  1. POIn-memory mime-type detection with Cocoa (OS X)?
    text
    copied!<p>My application is a viewer for a custom format, a zip file with a well defined XML manifest and resources, such as images and movies. I use zlib to open up the zip file in memory and then proceed to display said resources. </p> <p>One problem I've ran into is that I'm unable to properly display videos, apparently because QTMovie cannot determine the mime-type. Movie loaded from file ([QTMovie movieWithFile]) works perfectly. Loaded from memory ([QTMovie movieWithData]) refuses to work. </p> <p>This makes sense, because lacking the file extension, QTMovie cannot determine the mime-type information. After a bit of a search, I've resorted to using QTDataReference in the following mannner:</p> <pre><code>NSData *movieData = ...read from memory...; QTDataReference *movieDataReference = [[QTDataReference alloc] initWithReferenceToData:movieData name:fileName MIMEType:@"video/x-m4v"]; QTMovie *mov = [QTMovie movieWithDataReference:movieDataReference error:&amp;err]; </code></pre> <p>This works nicely, however hardcoding MIMEType is far from ideal. I have access to the filename and the extension, so I've attempted to find the mime-type using UTI (thanks to the nice folks on #macdev):</p> <pre><code>- (NSString*)mimeTypeForExtension:(NSString*)ext { CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,(CFStringRef)ext,NULL); return NSMakeCollectable(UTTypeCopyPreferredTagWithClass((CFStringRef)UTI,kUTTagClassMIMEType)); } </code></pre> <p>This however doesn't work. Clearly, there's an internal OS X database of extensions and corresponding mime-types, somewhere. Otherwise from-disk movies wouldn't work. How do I get access to it?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload