尝试获取 SPTArtist 对象和相关封面作品时出现 artistWithURI 异常

artistWithURI exception when trying to obtain SPTArtist object and associated cover artwork

我正在尝试使用 Spotify iOS SDK 获取封面图片。据我了解 API,SPTArtist 对象有用于最小和最大 SPTImage 的便捷方法。当 audioStreaming 控制器委托在新曲目的开头触发时,它会为包含 SPTAudioStreamingMetadataArtistURI 的当前歌曲提供曲目元数据。我正在尝试使用此 URI 通过以下方式获取这首歌的 SPTArtist 对象:

-(void)audioStreaming:(SPTAudioStreamingController *)audioStreaming didChangeToTrack:(NSDictionary *)trackMetadata {
    [SPTArtist artistWithURI:trackMetadata[SPTAudioStreamingMetadataArtistURI] session:[SpotifySettingsHelper session] callback:^(NSError *error, id object) {
        ...
        // asynchronously obtain the image
        NSData *imageData = [NSData dataWithContentsOfURL:artist.largestImage.imageURL options:0 error:&error];
        ...
    }];
}

[SPTArtist artistWithURI:session:callback] 调用崩溃并出现以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString absoluteString]: unrecognized selector sent to instance 0x7f9ed0ebe4d0'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010d43ff35 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010d0d8bb7 objc_exception_throw + 45
2   CoreFoundation                      0x000000010d44704d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x000000010d39f27c ___forwarding___ + 988
4   CoreFoundation                      0x000000010d39ee18 _CF_forwarding_prep_0 + 120
5   MyGreatApp                        0x000000010a08a053 __52+[SPTRequest requestItemAtURI:withSession:callback:]_block_invoke + 160
6   libdispatch.dylib                   0x000000010d919ba6 _dispatch_call_block_and_release + 12
7   libdispatch.dylib                   0x000000010d9377f4 _dispatch_client_callout + 8
8   libdispatch.dylib                   0x000000010d92215d _dispatch_root_queue_drain + 1097
9   libdispatch.dylib                   0x000000010d9235d9 _dispatch_worker_thread3 + 111
10  libsystem_pthread.dylib             0x000000010dcbb6cb _pthread_wqthread + 729
11  libsystem_pthread.dylib             0x000000010dcb94a1 start_wqthread + 13

)

这种方法有什么问题?如果这不是正确的方法,我应该如何获得艺术品?

您似乎正在将 NSString 传递给需要 NSURL 的 API。检查 trackMetadata[SPTAudioStreamingMetadataArtistURI] returns — 如果是 NSString,请使用 [NSURL urlWithString:] 进行转换。

此外,在您的代码中您编写 // asynchronously obtain the image 然后执行同步任务。这会阻塞你的主线程很长一段时间。