iOS使用ID3Lib来编辑NSDocsDir中的mp3文件的ID3标签

我正在使用ID3Lib示例项目来编辑mp3文件上的标题,专辑和艺术家ID3标签,一切都很好,直到我来添加一个图像(封面艺术),如果任何人有任何想法如何完成下面的代码将是伟大的:

- (void)demo { nImage = [UIImage imageNamed:@"amazing-grace.jpg"];//unused NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"amazing-grace" ofType:@"jpg"]; NSString *path = [[NSBundle mainBundle] pathForResource:@"amazing-grace-10s" ofType:@"mp3"]; NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docPath = [[docPaths objectAtIndex:0] stringByAppendingPathComponent:@"amazing-grace-10s.mp3"]; [[NSFileManager defaultManager] copyItemAtPath:path toPath:docPath error:nil]; // Read title tag ID3_Tag tag; tag.Link([path UTF8String]); ID3_Frame *titleFrame = tag.Find(ID3FID_TITLE); unicode_t const *value = titleFrame->GetField(ID3FN_TEXT)->GetRawUnicodeText(); NSString *title = [NSString stringWithCString:(char const *) value encoding:NSUnicodeStringEncoding]; NSLog(@"The title before is %@", title); // Write title tag tag.Link([docPath UTF8String]); tag.Strip(ID3FID_TITLE); tag.Strip(ID3FID_ALBUM); tag.Strip(ID3FID_LEADARTIST); tag.Clear(); ID3_Frame frame; frame.SetID(ID3FID_TITLE); frame.GetField(ID3FN_TEXTENC)->Set(ID3TE_UNICODE); NSString *newTitle = nTitle; frame.GetField(ID3FN_TEXT)->Set((unicode_t *) [newTitle cStringUsingEncoding:NSUTF16StringEncoding]); ID3_Frame frame2; frame2.SetID(ID3FID_ALBUM); frame2.GetField(ID3FN_TEXTENC)->Set(ID3TE_UNICODE); NSString *newAlbum = nAlbmum; frame2.GetField(ID3FN_TEXT)->Set((unicode_t *) [newAlbum cStringUsingEncoding:NSUTF16StringEncoding]); ID3_Frame frame3; frame3.SetID(ID3FID_LEADARTIST); frame3.GetField(ID3FN_TEXTENC)->Set(ID3TE_UNICODE); NSString *newArtist = nArtist; frame3.GetField(ID3FN_TEXT)->Set((unicode_t *) [newArtist cStringUsingEncoding:NSUTF16StringEncoding]); //this is the image code ID3_Frame frame4; frame4.SetID(ID3FID_PICTURE); frame4.GetField(ID3FN_TEXTENC)->Set(ID3TE_UNICODE);// dont think this should be TEXTENC NSString *newImage = imagePath; frame4.GetField(ID3FN_DATA)->FromFile((const char *)[newImage cStringUsingEncoding:NSUTF16StringEncoding]);//this line is also probably wrong tag.AddFrame(frame); tag.AddFrame(frame2); tag.AddFrame(frame3); tag.AddFrame(frame4); tag.SetPadding(false); tag.SetUnsync(false); tag.Update(ID3TT_ID3V2); NSLog(@"The title after is %@ The album after is %@ The artist after is %@ The artist after is %@", newTitle,newAlbum,newArtist,newImage); } 

使用UTF-8编码的文件path,并将正常工作:

 frame.GetField(ID3FN_DATA)->FromFile((const char *)[newImage cStringUsingEncoding:NSUTF8StringEncoding]); 

我得到这个代码,从这个来源: https : //android.googlesource.com/platform/external/id3lib/+/8a58cac9f8db5ed55b02b3ac24156d628af923d1/examples/test_pic.cpp

 frame.SetID(ID3FID_PICTURE); frame.GetField(ID3FN_MIMETYPE)->Set("image/jpeg"); frame.GetField(ID3FN_PICTURETYPE)->Set(11); frame.GetField(ID3FN_DESCRIPTION)->Set("B/W picture of Saint-Saëns"); //frame.GetField(ID3FN_DATA)->FromFile("composer.jpg"); frame.GetField(ID3FN_DATA)->FromFile((const char *)[newImage cStringUsingEncoding:NSUTF8StringEncoding]); tag.AddFrame(frame); 

和! 这个工作很好。 :d