2013-03-25 1 views
6

반송 된 미디어 유형이 -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"] 

누구나이 작업을 수행하는 방법이 다릅니다. 미디어 유형을 확인하기 위해 권장되는 방법은 무엇입니까? "모든 이미지 유형"또는 "모든 비디오 유형"을 포함하는 방법이 바람직합니다.

답변

2

제가 첫번째 방법 UTTypeEqual 제 2 및 제 3 방법 (NSString 비교 예) 사이의 차이, 또는 CFStringRefsNSStrings 다루는 선호도의 문제라고한다.

네 번째 유형은 실제 문자열 값으로 kUTTypeMovie을 참조하는 것으로 보입니다.이 유형은 사적인 것으로 실제로 변경 될 수 있으므로 절대 수행하지 않아야합니다. 그 외에도 그것은 두 번째와 세 번째 방법과 같습니다.

당신이 원했던 /해야 할 필요성에 따라 아마 몇 가지 유형을 확인하고 싶을 것 같습니다.

아마 kUTTypeAudiovisualContent, KUTTypeMovie, KUTTypeVideo, kUTTypeQuickTimeMovie, kUTTypeMPEG, kUTTypeMPEG4을 확인할 것입니다. 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. 
13

에서

전체 목록 대신 특정 UTI로 적합성를 확인하기 위해 더 좋을 것이다.

지금 iOS에서 내게 public.movie이라고 알려주지 만 내년에는 무엇을 말할까요? public.video을 확인하는 사용자가 표시됩니다. 좋습니다. 그렇기 때문에 하나가 아닌 두 가지 유형을 하드 코딩했습니다.

하지만 "이 영화입니까?" iOS가 반환 할 특정 유형의 하드 코드가 아닌가요? 그렇게 할 수있는 방법이있다 : 영화가 (에 관계없이 어느 특정 유형이 반환됩니다) 반환되는 경우 mediaType 매개 영화를 나타내는 경우 모든 영화 kUTTypeMovie에서 일치하기 때문에

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

이 방법을 사용하여이 isMovieYES을해야합니다. 매우 명확하게하기 위해 kUTTypeVideo 인 경우 kUTTypeVideokUTTypeMovie을 따르므로이 값을 영화로 인식합니다.이미지가 반환되는 경우 모든 이미지 kUTTypeImage을 준수하기 때문에

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

isIamgeYES해야한다 :

마찬가지로, 당신은 반환되는 것은 이미지가 있는지를 확인할 수 있습니다.

여기 애플의 (부분) 유형 트리를 참조하십시오. Uniform Type Identifiers Are Declared in a Conformance Hierarchy.

: 당신은 public.video는 다음과 같이 정의되어 볼 수 있습니다, 특히

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

: 모든 요로 감염의 덜 유용하지만 더 전체 목록은 현재 시스템에서 인식하고 쉘에서 자신의 적합성과를 얻을 수 있습니다

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

유형이 동일하면 UTTypeConformsTotrue을 반환합니다. Apple의 문서에서 :

균일 한 형식 식별자가 두 번째 형식과 같거나 일치하면 true를 반환합니다.

+1

잘 모르겠다. 이것이 제가 지금까지 본 최선의 접근법입니다. – Toby

+0

나는 당신의 접근 방식도 좋아합니다. Thx는이 솔루션에 많이 사용됩니다. – iWheelBuy

+0

일단 이러한 API를 비틀 거리다 보면 실제로 의미가있는 유일한 API입니다. :) –