Mac OS X에서 저장 장치의 아이콘을 어떻게 얻을 수 있습니까?

StackOverflow https://stackoverflow.com/questions/580002

  •  06-09-2019
  •  | 
  •  

문제

내 장치를 사용하여 찾았습니다 IOServiceGetMatchingServices 그리고 다음과 같은 부동산 사전을 얻었습니다.

kernResult = IORegistryEntryCreateCFProperties(nextMedia,
                    (CFMutableDictionaryRef *)&props,
                                              kCFAllocatorDefault, 0);

그 사전에서 아이콘에 대한 정보를 추출 할 수 있습니다.

NSString *bId = [props valueForKeyPath:@"IOMediaIcon.CFBundleIdentifier"];
NSString *rFile = [props valueForKeyPath:@"IOMediaIcon.IOBundleResourceFile"];

그 두 사람은 나에게 이것을 줘 (예 : 예를 들어) :

com.apple.iokit.IOStorageFamily   (Bundle identifier)
Internal.icns                     (Resource File)

이 방법을 사용하여 아이콘을 추출하려고했습니다.

NSBundle *bundleWithIcon = [NSBundle bundleWithIdentifier:bId];
NSString *iconPath = [bundleWithIcon pathForResource:rFile ofType:nil];

하지만 bundleWithIcon ~이다 nil.

이것이 아이콘을 얻는 올바른 방법입니까?

나는 그것을 어떻게 든로드 할 수 있도록 번들을로드해야한다고 생각합니다. bundleWithIdentifier, 어떻게 할 수 있습니까?

추신: 또 다른 질문이 있습니다 어느 곳에서는 같은 것을 요청하지만, 이것이 올바른 방법이 아니라는 것이 아니라 다발을 요구합니다.

도움이 되었습니까?

해결책 2

최근에 앤드류 미릭도 비슷한 질문에 대답했습니다 Darwin-Dev 메일 링리스트 :

KextManagerCreateURLForBundleIdentifier()안에 <IOKit/kext/KextManager.h> 비록 사용될 수 있지만, 나는 그것이 1)로드 된 kexts에만 효과가 있다고 생각합니다. 또는 2)/s/l/e/. 다음은 Snow Leopard Headerdoc입니다.

/*!
 * @function KextManagerCreateURLForBundleIdentifier
 * @abstract Create a URL locating a kext with a given bundle identifier.
 *
 * @param    allocator
 *           The allocator to use to allocate memory for the new object.
 *           Pass <code>NULL</code> or <code>kCFAllocatorDefault</code>
 *           to use the current default allocator.
 * @param    kextIdentifier
 *           The bundle identifier to look up.
 *
 * @result
 * A CFURLRef locating a kext with the requested bundle identifier.
 * Returns <code>NULL</code> if the kext cannot be found, or on error.
 *
 * @discussion
 * Kexts are looked up first by whether they are loaded, second by version.
 * Specifically, if <code>kextIdentifier</code> identifies a kext
 * that is currently loaded,
 * the returned URL will locate that kext if it's still present on disk.
 * If the requested kext is not loaded,
 * or if its bundle is not at the location it was originally loaded from,
 * the returned URL will locate the latest version of the desired kext,
 * if one can be found within the system extensions folder.
 * If no version of the kext can be found, <code>NULL</code> is returned.
 */
CFURLRef KextManagerCreateURLForBundleIdentifier(
    CFAllocatorRef allocator,
    CFStringRef    kextIdentifier);

스노우 레오파드 이전에는/s/l/e의 kexts에만 효과가있을 수 있습니다. API는 존재했지만 그 행동을 설명하는 Headerdoc은 없었습니다.

나에게 이것은 Mac OS X 10.5에서 정말 잘 작동했습니다.

다른 팁

nsworkspace를 사용할 수 있습니다.
초기 이미지는 32x32이지만 다른 크기에 대한 표현이 있으며 그에 따라 스케일링됩니다.

NSWorkspace * ws = [NSWorkspace sharedWorkspace];
NSImage * icon = [ws iconForFile:@"/Volumes/Whatever"];
NSLog(@"%@", [icon representations]); // see what sizes the icon has
icon.size = NSMakeSize(512, 512);

이것은 당신을 도울 수 있습니다. (또는 아마도 아닐 수도 있습니다) ...

'ioreg'명령을 가지고 놀면서 나는 당신의 질문을 생각 나게하는 무언가를 발견했고, 그래서 나는 그것을 게시 할 것입니다.

다음 명령을 발행해보십시오.

ioreg -c IOMedia -x

이는 다음과 같이 보이는 출력의 큰 혼란을 일으킬 것입니다.

  |     +-o IOBlockStorageDriver  <class IOBlockStorageDriver, registered, matched, active, busy 0, retain 7>
  |       +-o Apple read/write Media  <class IOMedia, registered, matched, active, busy 0, retain 9>
  |         | {
  |         |   "Removable" = Yes
  |         |   "BSD Unit" = 0x4
  |         |   "IOBusyInterest" = "IOCommand is not serializable"
  |         |   "BSD Minor" = 0xc
  |         |   "Ejectable" = Yes
  |   |         |   "BSD Name" = "disk4"
  |         |   "Leaf" = No
  |         |   "IOMediaIcon" = {"CFBundleIdentifier"="com.apple.iokit.IOStorageFamily","IOBundleResourceFile"="Removable.icns"}
  |         |   "Preferred Block Size" = 0x200
  |         |   "Whole" = Yes
  |         |   "Open" = Yes
  |         |   "Size" = 0x100000
  |         |   "Writable" = Yes
  |         |   "Content" = "Apple_partition_scheme"
  |         |   "IOGeneralInterest" = "IOCommand is not serializable"
  |         |   "Content Hint" = ""
  |         | }         |   "BSD Major" = 0xe

이 모든 것이 'IOMEDIA'와의 IO 레지스트리 트리를 가로 지르면 "iOMEDIAICON"을 포함하는 속성 사전을 얻을 수 있다고 믿게됩니다. 번들 식별자 및 리소스 파일 이름을 알려주는 수집.

이것이 쉽다고 말하지는 않지만 ... FireWire SDK 필요한 모든 예제 코드의 경우 ... 어쨌든 미리 채워진 경로 (향후 OS 릴리스에서 사라질 수 있음)보다 "더 나은"것일 수 있습니다.

| k

그 두 사람은 나에게 이것을 줘 (예 : 예를 들어) :

com.apple.iokit.iostorageFamily (번들 식별자) internal.icns (리소스 파일)

이 방법을 사용하여 아이콘을 추출하려고했습니다.

NSBUNDLE *번들 WITHICON = [NSBUNDLE BUNDLEWITHINDIFIER : BID]; nsstring *iconpath = [bundlewithicon pathforResource : rfile oftype : nil];

하지만 bundleWithIcon ~이다 nil.

bundleWithIdentifier: 해당 식별자가있는 번들에 대한 NSBundle 인스턴스를 이미 작성해야합니다. 이전에 만들어진 인스턴스 만 찾습니다. 따라서 파일 시스템에서 번들을 찾아 PathName으로 인스턴스화해야합니다. 다행히도, 당신은 이미 그것에 대한 질문에 대한 링크가있는 것 같습니다.

사용자 정의 아이콘이없는 볼륨은 여기에서 하드 코딩 된 OS의 일반 아이콘 중 하나와 함께 표시됩니다. 사용자 정의 아이콘이있는 볼륨은 해당 아이콘을 파일 시스템에 저장하고 장착되지 않은 경우 전적으로 작업이됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top