有没有办法从ios中的mp3中提取嵌入的图像数据 [英] Is there a way to extract embedded image data from an mp3 in ios

查看:123
本文介绍了有没有办法从ios中的mp3中提取嵌入的图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,mp3有时会包含专辑封面(在我的情况下,我正在使用播客文件)。在iOS中有没有办法从mp3文件中提取图像数据?

I understand that mp3s sometimes contain album artwork (in my case, I'm working with podcast files). Is there a way in iOS to extract the image data from an mp3 file?

推荐答案

MP3,包括播客,经常嵌入元数据,包括艺术品。获取嵌入元数据(对于不在iTunes库中的MP3)的最简单方法是通过 AVAsset 类。如果您尝试通过网络获取元数据,请使用 AVURLAsset ,但访问元数据与其中任何一个都相同。

MP3s, including podcasts, do often have embedded metadata, including artwork. The easiest way to get the embedded metadata (for MP3s not in the iTunes library) is through the AVAsset class. If you are trying to get metadata over a network use the AVURLAsset instead, but accessing the metadata is the same with either.

AVAsset 有一个名为<$ c的 NSArray 属性$ c> commonMetadata ,其中包含类 AVMetadataItem 的实例。你可以迭代这个数组,找到 commonKey 的元素(通常) artwork value AVMetadataItem 的属性应该是关于该图稿的 NSDictionary 数据。在这个字典中你会找到键 data ,它应该包含你可以用来构建 UIImage 的数据。这是完全嵌套的,这种描述无疑是令人困惑的。所以这是一个代码示例。

AVAsset has an NSArray property named commonMetadata which contains instances of the class AVMetadataItem. You can iterate over this array to find the element with the commonKey of(usually) "artwork" The value property of this AVMetadataItem should be an NSDictionary of data about the artwork. Within this dictionary you will find the key "data" which should contain data you can use to construct a UIImage. This is quite nested and this description is admittedly confusing. So here is a code example.

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 。另外请注意此次通话的阻止性质。

Again this is a very simple example of how to load from a local resource; If you are loading this over a network you should use an AVURLAsset. Also be careful of the blocking nature of this call.

这篇关于有没有办法从ios中的mp3中提取嵌入的图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆