iphone - 有没有办法从 ios 中的 mp3 中提取嵌入的图像数据

标签 iphone objective-c ios image mp3

我知道 mp3 有时包含专辑封面(在我的例子中,我正在处理播客文件)。 iOS 有没有办法从 mp3 文件中提取图像数据?

最佳答案

MP3(包括播客)通常包含嵌入式元数据,包括艺术作品。获取嵌入元数据(对于不在 iTunes 库中的 MP3)的最简单方法是通过 AVAsset 类。如果您尝试通过网络获取元数据,请改用 AVURLAsset,但访问元数据与两者相同。

AVAsset 有一个名为 commonMetadataNSArray 属性,它包含类 AVMetadataItem 的实例。您可以遍历此数组以查找具有(通常)“artwork”的 commonKey 的元素 此 AVMetadataItem 的 value 属性 应该是关于艺术品的数据的 NSDictionary。在此字典中,您会找到关键字“data”,它应该包含可用于构建 UIImage 的数据。这是相当嵌套的,而且这种描述无疑是令人困惑的。所以这是一个代码示例。

NSURL *url = <# url of resource here #>;
AVAsset *asset = [AVAsset assetWithURL:url];
for (AVMetadataItem *metadataItem in asset.commonMetadata) {
    if ([metadataItem.commonKey isEqualToString:@"artwork"]){
        NSDictionary *imageDataDictionary = (NSDictionary *)metadataItem.value;
        NSData *imageData = [imageDataDictionary objectForKey:@"data"];
        UIImage *image = [UIImage imageWithData:imageData];
         // Display this image on my UIImageView property imageView
        self.imageView.image = image;
    }
}

这又是一个如何从本地资源加载的非常简单的示例;如果您通过网络加载它,您应该使用 AVURLAsset。还要注意此调用的阻塞性质。

关于iphone - 有没有办法从 ios 中的 mp3 中提取嵌入的图像数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7814248/

相关文章:

iphone - Zbar SDK 在 iOS6 中运行不佳

iphone - NSDate - iPhone 上的 GMT

objective-c - CGContextShowTextAtPoint 和西里尔文字

ios - UILocalNotification 的 alertTitle 属性不起作用

ios - Firestore.firestore() 失败并显示 "Type ' Firestore' 没有成员 'firestore'“

ios - 未出现的 View Controller

ios - 有没有关于 cocoa touch 自动化 UI 测试的好教程?

iPhone NSDateComponent 日期错误 1.1.2011?

iphone - Objective C 语言的 Dicom 图像处理库

ios - 发送带有字符串的后请求