7

我见过各种检查返回的媒体类型是否-imagePickerController:didFinishPickingMediaWithInfo:为视频的方法。比如我的方式:

- (void)imagePickerController:(UIImagePickerController *)imagePicker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if (UTTypeEqual(kUTTypeMovie, 
    (__bridge CFStringRef)[info objectForKey:UIImagePickerControllerMediaType])) 
    {
        // ...
    }
}

或者

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {

或者

if ([mediaType isEqualToString:(NSString *)kUTTypeVideo] || 
    [mediaType isEqualToString:(NSString *)kUTTypeMovie])

或者

if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0)
        == kCFCompareEqualTo) 

或者

if ([mediaType isEqualToString:@"public.movie"]

每个人似乎都有不同的方法来做到这一点。检查媒体类型的推荐方法是什么?最好采用包含“所有图像类型”或“所有视频类型”的方式。

4

2 回答 2

16

最好检查与特定 UTI的一致性。

现在,iOS 告诉你它是 a public.movie,但明年它会说什么呢?你会看到人们public.video也在检查。太好了,所以你硬编码了两种类型而不是一种。

但是问“这是一部电影吗?”不是更好吗?而不是硬编码您认为 iOS 将返回的特定类型?有一种方法可以做到这一点:

NSString *mediaType = info[UIImagePickerControllerMediaType];
BOOL isMovie = UTTypeConformsTo((__bridge CFStringRef)mediaType,
                                kUTTypeMovie) != 0;

使用这种方法,isMovie应该是YES如果返回电影(无论返回哪种特定类型)如果 mediaType 表示电影,因为所有电影都符合kUTTypeMovie. 要非常清楚,如果是 a kUTTypeVideothis 也会将其识别为电影,因为kUTTypeVideo符合kUTTypeMovie.

同样,您可以检查返回的东西是否是图像:

NSString *mediaType = info[UIImagePickerControllerMediaType];
BOOL isImage = UTTypeConformsTo((__bridge CFStringRef)mediaType,
                                kUTTypeImage) != 0;

isIamge如果返回图像应该YES是,因为所有图像都符合kUTTypeImage.

在此处查看 Apple 的(部分)类型树:Uniform Type Identifiers Are Declared in a Conformance Hierarchy。您可以获得一个不太有用但更完整的列表,其中列出了您的系统当前识别的所有 UTI 及其与 shell 的一致性:

/System/Library/Frameworks/CoreServices.framework/Frameworks\
/LaunchServices.framework/Versions/A/Support/lsregister -dump

特别是,您可以看到 public.video 是这样定义的:

--------------------------------------------------------
type    id:            8344
    uti:           public.video
    description:   video
    flags:         exported  active  core  apple-internal  trusted  
    icon:          
    conforms to:   public.movie
    tags:          
--------------------------------------------------------

请注意,如果类型也相同,则UTTypeConformsTo返回。true来自 Apple 的文档:

如果统一类型标识符等于或符合第二种类型,则返回 true。

于 2015-03-31T20:01:03.780 回答
2

I would say the difference between the first method UTTypeEqual and the second and third methods (NSString comparison), is a matter of preference in dealing with CFStringRefs or NSStrings.

The 4th type appears to be referencing kUTTypeMovie by its actual string value which you should absolutely not do as it is private and may actually change. Aside from that it is just like the second and third methods.

It does look like you'll probably want to check against a few more types depending on how thorough you want/need to be.

I would probably check for kUTTypeAudiovisualContent, KUTTypeMovie, KUTTypeVideo, kUTTypeQuickTimeMovie, kUTTypeMPEG, kUTTypeMPEG4.

Full list from UTType Reference

kUTTypeAudiovisualContent
An abstract type identifier for audio and/or video content.
Available in iOS 3.0 and later.
Declared in UTCoreTypes.h.

kUTTypeMovie
An abstract type identifier for a media format which may contain both video and audio. Corresponds to what users would label a "movie"
Available in iOS 3.0 and later.
Declared in UTCoreTypes.h.

kUTTypeVideo
An abstract type identifier for pure video data(no audio).
Available in iOS 3.0 and later.
Declared in UTCoreTypes.h.

kUTTypeAudio
An abstract type identifier for pure audio data (no video).
Available in iOS 3.0 and later.
Declared in UTCoreTypes.h.

kUTTypeQuickTimeMovie
The type identifier for a QuickTime movie.
Available in iOS 3.0 and later.
Declared in UTCoreTypes.h.

kUTTypeMPEG
The type identifier for a MPEG-1 or MPEG-2 movie.
Available in iOS 3.0 and later.
Declared in UTCoreTypes.h.

kUTTypeMPEG4
The type identifier for a MPEG-4 movie.
Available in iOS 3.0 and later.
Declared in UTCoreTypes.h.

kUTTypeMP3
The type identifier for MP3 audio.
Available in iOS 3.0 and later.
Declared in UTCoreTypes.h.

kUTTypeMPEG4Audio
The type identifier for a MPEG-4 audio layer (.m4a, or the MIME type audio/MP4).
Available in iOS 3.0 and later.
Declared in UTCoreTypes.h.

kUTTypeAppleProtectedMPEG4Audio
The type identifier for Apple protected MPEG4 format (.m4p, iTunes music store format).
Available in iOS 3.0 and later.
Declared in UTCoreTypes.h.
于 2014-03-07T09:50:58.990 回答