iOS AVFoundation: How do I fetch artwork from an mp3 file? - objective-c

My code:
- (void)metadata {
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.fileURL options:nil];
NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon];
NSArray *titles = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
NSArray *artists = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtist keySpace:AVMetadataKeySpaceCommon];
NSArray *albumNames = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyAlbumName keySpace:AVMetadataKeySpaceCommon];
AVMetadataItem *artwork = [artworks objectAtIndex:0];
AVMetadataItem *title = [titles objectAtIndex:0];
AVMetadataItem *artist = [artists objectAtIndex:0];
AVMetadataItem *albumName = [albumNames objectAtIndex:0];
if ([artwork.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
NSDictionary *dictionary = [artwork.value copyWithZone:nil];
self.currentSongArtwork = [UIImage imageWithData:[dictionary objectForKey:#"data"]];
}
else if ([artwork.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
self.currentSongArtwork = [UIImage imageWithData:[artwork.value copyWithZone:nil]];
}
self.currentSongTitle = [title.value copyWithZone:nil];
self.currentSongArtist = [artist.value copyWithZone:nil];
self.currentSongAlbumName = [albumName.value copyWithZone:nil];
self.currentSongDuration = self.audioPlayer.duration;
}
This works for fetching artwork from m4a files, but doesn’t work for mp3 files. If the asset points to an mp3 file, artworks is empty. What am I doing wrong and how do I fix it?

I found that the artworks were being loaded asynchronously while the image was being set synchronously. I solved it this way:
- (void)metadata {
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.fileURL options:nil];
NSArray *titles = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
NSArray *artists = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtist keySpace:AVMetadataKeySpaceCommon];
NSArray *albumNames = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyAlbumName keySpace:AVMetadataKeySpaceCommon];
AVMetadataItem *title = [titles objectAtIndex:0];
AVMetadataItem *artist = [artists objectAtIndex:0];
AVMetadataItem *albumName = [albumNames objectAtIndex:0];
NSArray *keys = [NSArray arrayWithObjects:#"commonMetadata", nil];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
withKey:AVMetadataCommonKeyArtwork
keySpace:AVMetadataKeySpaceCommon];
for (AVMetadataItem *item in artworks) {
if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
NSDictionary *d = [item.value copyWithZone:nil];
self.currentSongArtwork = [UIImage imageWithData:[d objectForKey:#"data"]];
} else if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
self.currentSongArtwork = [UIImage imageWithData:[item.value copyWithZone:nil]];
}
}
}];
self.currentSongTitle = [title.value copyWithZone:nil];
self.currentSongArtist = [artist.value copyWithZone:nil];
self.currentSongAlbumName = [albumName.value copyWithZone:nil];
self.currentSongDuration = self.audioPlayer.duration;
}

I just found the answer to that here: How can I extract metadata from mp3 file in ios development and was now looking to see why that code doesn't work on an m4a file.
I took a little bit from your code and present the following which seems to work for both m4a and mp3. Note that I left the code which works for mp3 as an open else clause without the qualifier - if anybody gains more experience with this they are welcome to refine!
- (void)getMetaDataForSong:(MusicItem *)musicItem {
AVAsset *assest;
// filePath looks something like this: /var/mobile/Applications/741647B1-1341-4203-8CFA-9D0C555D670A/Library/Caches/All Summer Long.m4a
NSURL *fileURL = [NSURL fileURLWithPath:musicItem.filePath];
NSLog(#"%#", fileURL);
assest = [AVURLAsset URLAssetWithURL:fileURL options:nil];
NSLog(#"%#", assest);
for (NSString *format in [assest availableMetadataFormats]) {
for (AVMetadataItem *item in [assest metadataForFormat:format]) {
if ([[item commonKey] isEqualToString:#"title"]) {
musicItem.strSongTitle = (NSString *)[item value];
}
if ([[item commonKey] isEqualToString:#"artist"]) {
musicItem.strArtistName = (NSString *)[item value];
}
if ([[item commonKey] isEqualToString:#"albumName"]) {
musicItem.strAlbumName = (NSString *)[item value];
}
if ([[item commonKey] isEqualToString:#"artwork"]) {
UIImage *img = nil;
if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
img = [UIImage imageWithData:[item.value copyWithZone:nil]];
}
else { // if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
NSData *data = [(NSDictionary *)[item value] objectForKey:#"data"];
img = [UIImage imageWithData:data] ;
}
musicItem.imgArtwork = img;
}
}
}
}

As Ryan Heitner pointed out in a comment to Andy Weinstein, the way to retrieve the binary artwork data from the AVMetadataKeySpaceID3 has changed somewhere from IOS7 to IOS8. Casting blindly item to NSDictionary will crash in the ID3 case nowadays. To make code bullet proof just check dataValue, otherwise the Classes. In fact today item.value points to item.dataValue and has the NSData class.
- (UIImage*)imageFromItem:(AVMetadataItem*)item
{
NSData* data=nil;
if (item.dataValue!=nil) {
data=item.dataValue;
} else if ([item.value isKindOfClass:NSData.class]) { //never arrive here nowadays
data=(NSData*)item.value;
} else if ([item.value isKindOfClass:NSDictionary.class]) { //never arrive here nowadays...
NSDictionary* dict=(NSDictionary*)item.value;
data=dict[#"data"];
}
if (data==nil) {
return nil;
}
return [UIImage imageWithData:data];
}

Related

Get all Images from Live Photo

I want to get a NSArray with all the UIImage from a Live Photo to create a GIF of that. I tried to make screenshots while animating the live photo but it doesn't work.
Can anyone help me?
Thanks!
First step, you need convert a Live Photo to Video, using this:
PHAssetResourceManager.defaultManager().writeDataForAssetResource(assetRes,
toFile: fileURL, options: nil, completionHandler:
{
// Video file has been written to path specified via fileURL
}
Finally, using this library to convert this to GIF, or you can search in google for another way: https://github.com/NSRare/NSGIF
Hope this will help you.
This is what I did to achieve the same thing as you requested.
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"creationDate" ascending:NO]];
options.predicate = [NSPredicate predicateWithFormat:#"mediaType == %d", PHAssetMediaTypeImage];
options.predicate = [NSPredicate predicateWithFormat:#"mediaSubtype == %d", PHAssetMediaSubtypePhotoLive];
options.includeAllBurstAssets = NO;
PHFetchResult *allLivePhotos = [PHAsset fetchAssetsWithOptions:options];
NSLog(#"Get total live count : %ld",(unsigned long)allLivePhotos.count);
NSMutableArray *arrAllLiveImagesGroups = [NSMutableArray array];
for (PHAsset *asset in allLivePhotos) {
[asset requestContentEditingInputWithOptions:nil
completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
NSURL *urlMov = [contentEditingInput.livePhoto valueForKey:#"videoURL"];
NSMutableArray *arrLive = [NSMutableArray array];
NSMutableArray *arrSingleLiveImagesGroup = [NSMutableArray array];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:urlMov options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.requestedTimeToleranceAfter = kCMTimeZero;
generator.requestedTimeToleranceBefore = kCMTimeZero;
for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) * 5 ; i++){
#autoreleasepool {
CMTime time = CMTimeMake(i, 5);
NSError *err;
CMTime actualTime;
CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&err];
UIImage *generatedImage = [[UIImage alloc] initWithCGImage:image scale:1.0 orientation:UIImageOrientationDown];
[arrLive addObject:generatedImage];
CGImageRelease(image);
}
}
[arrSingleLiveImagesGroup addObject:arrLive];
[arrAllLiveImagesGroups addObject:arrSingleLiveImagesGroup];
}];
}

How to trim Music Library file and save it document directry

-(BOOL)trimAudioFileAtPath:(NSString *)inputFilename
start:(float)start
end:(float) stop{
NSString *outputFilename = #"File Path";
NSError *error = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:outputFilename]) {
if (![fileManager removeItemAtPath:outputFilename error:&error]) {
DebugLog(#"error file remove:%#",error); } else {
DebugLog(#"sucess remove file");
} }
NSURL *audioFileInput = [NSURL fileURLWithPath:inputFilename];
NSURL *audioFileOutput = [NSURL fileURLWithPath:outputFilename];
if (!audioFileInput || !audioFileOutput){ return NO; }
[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
AVMutableComposition *mutableComposition = [AVMutableComposition composition]; // Create the video composition track.
AVMutableCompositionTrack *mutableCompositionAudioTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
NSLog(#"audioFileInput %#",audioFileInput); AVURLAsset *assetUrl = [AVURLAsset assetWithURL:audioFileInput];
if ([[assetUrl tracksWithMediaType:AVMediaTypeAudio] count]==0) { return NO; }
// Get the first music track from each asset. AVAssetTrack *audioAssetTrack = [[assetUrl tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; [mutableCompositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,audioAssetTrack.timeRange.duration) ofTrack:audioAssetTrack atTime:kCMTimeZero error:nil];
// we need the audio asset to be at least 50 seconds long for this snippet
CMTime startTime = CMTimeMake(start, 1);
CMTime stopTime = CMTimeMake(stop, 1);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime,stopTime);
Float64 duration = CMTimeGetSeconds(exportTimeRange.duration);
// Create the export session with the composition and set the preset to the highest quality.
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:mutableComposition presetName:AVAssetExportPresetAppleM4A];
if (duration > 6.0){
AVMutableAudioMix *mutableAudioMix = [AVMutableAudioMix audioMix];
// Create the audio mix input parameters object.
AVMutableAudioMixInputParameters *mixParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:mutableCompositionAudioTrack];
// float totalDutaion=mutableComposition.duration.value;
float totalDutaion=duration;
float lenth=totalDutaion/3;
CMTime startCM = CMTimeMake(totalDutaion-lenth-1,mutableComposition.duration.timescale);
CMTime endCM = CMTimeMake(lenth, mutableComposition.duration.timescale);
// Set the volume ramp to slowly fade the audio out over the duration of the composition.
[mixParameters setVolumeRampFromStartVolume:0.f toEndVolume:1.f timeRange:CMTimeRangeMake(startTime, endCM)];
[mixParameters setVolumeRampFromStartVolume:1.f toEndVolume:0.f timeRange:CMTimeRangeMake(startCM,endCM)];
// Attach the input parameters to the audio mix.
mutableAudioMix.inputParameters = #[mixParameters];
exportSession.audioMix=mutableAudioMix; }
if (exportSession == nil){
return NO; }
exportSession.outputURL = audioFileOutput; exportSession.outputFileType = AVFileTypeAppleM4A; exportSession.timeRange = exportTimeRange;
[exportSession exportAsynchronouslyWithCompletionHandler:^ {
if (AVAssetExportSessionStatusCompleted == exportSession.status)
{
// It worked!
}
else if (AVAssetExportSessionStatusFailed == exportSession.status)
{
// It failed...
} }]; return YES; }
I am using this function to trim the music file from music libaray. The Above code is working file when I try to trim a file which is located in bundle. But When I try to use the same function with input file from iTune music Library it is give no Tracks i.e. if ([[assetUrl tracksWithMediaType:AVMediaTypeAudio] count]==0) {
return NO;
} return No. Can anyone help to trim music from iTunes Libaray
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
if (mediaItemCollection) {
NSLog(#"%#",[mediaItemCollection items]);
// [musicPlayer setQueueWithItemCollection: mediaItemCollection];
// [musicPlayer play];
}
[KVNProgress showWithStatus:#"Processing"];
MPMediaItem *item =mediaItemCollection.representativeItem;
NSURL* assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
// set up an AVAssetReader to read from the iPod Library
AVURLAsset *songAsset =
[AVURLAsset URLAssetWithURL:assetURL options:nil];
NSError *assetError = nil;
AVAssetReader *assetReader =[AVAssetReader assetReaderWithAsset:songAsset error:&assetError];
if (assetError) {
NSLog (#"error: %#", assetError);
return;
}
AVAssetReaderOutput *assetReaderOutput =[AVAssetReaderAudioMixOutput assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks
audioSettings: nil];
if (! [assetReader canAddOutput: assetReaderOutput]) {
NSLog (#"can't add reader output... die!");
return;
}
[assetReader addOutput: assetReaderOutput];
// NSArray *dirs = NSSearchPathForDirectoriesInDomains
// (NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDirectoryPath = [dirs objectAtIndex:0];
// NSString *exportPath = [documentsDirectoryPath stringByAppendingPathComponent:#"out.m4a"];
NSString * exportPath =[NSString stringWithFormat:#"%#%#", NSTemporaryDirectory(), #"out.m4a"];
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) {
[[NSFileManager defaultManager] removeItemAtPath:exportPath
error:nil];
}
NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
AVAssetWriter *assetWriter =[AVAssetWriter assetWriterWithURL:exportURL
fileType:AVFileTypeCoreAudioFormat
error:&assetError];
if (assetError) {
NSLog (#"error: %#", assetError);
return;
}
AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary *outputSettings =[NSDictionary dictionaryWithObjectsAndKeys:
#(kAudioFormatLinearPCM), AVFormatIDKey,
#44100.0, AVSampleRateKey,
#2, AVNumberOfChannelsKey,
[NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)],AVChannelLayoutKey,
#16, AVLinearPCMBitDepthKey,
#NO, AVLinearPCMIsNonInterleaved,
#NO,AVLinearPCMIsFloatKey,
#NO, AVLinearPCMIsBigEndianKey,
nil];
AVAssetWriterInput *assetWriterInput =[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
outputSettings:outputSettings];
if ([assetWriter canAddInput:assetWriterInput]) {
[assetWriter addInput:assetWriterInput];
} else {
NSLog (#"can't add asset writer input... die!");
return;
}
assetWriterInput.expectsMediaDataInRealTime = NO;
[assetWriter startWriting];
[assetReader startReading];
AVAssetTrack *soundTrack = [songAsset.tracks objectAtIndex:0];
CMTime startTime = CMTimeMake (0, soundTrack.naturalTimeScale);
[assetWriter startSessionAtSourceTime: startTime];
__block UInt64 convertedByteCount = 0;
dispatch_queue_t mediaInputQueue =
dispatch_queue_create("mediaInputQueue", NULL);
[assetWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue
usingBlock: ^
{
while (assetWriterInput.readyForMoreMediaData) {
CMSampleBufferRef nextBuffer =
[assetReaderOutput copyNextSampleBuffer];
if (nextBuffer) {
// append buffer
[assetWriterInput appendSampleBuffer: nextBuffer];
// update ui
convertedByteCount +=
CMSampleBufferGetTotalSampleSize (nextBuffer);
} else {
// done!
[assetWriterInput markAsFinished];
[assetWriter finishWritingWithCompletionHandler:^{
[assetReader cancelReading];
[self performSelectorOnMainThread:#selector(updateCompletedAtMusicPath:)
withObject:exportPath
waitUntilDone:NO];
// NSLog (#"done. file size is %llu",[outputFileAttributes fileSize]);
}];
break;
}}}];
[self dismissViewControllerAnimated:NO completion:^{
}];
}
This is code used for geting the URL form iTune Library and story in document directry
First i just want to point out that you can just read segments of the audio from the library by setting the timeRange property of your assetReader. This way instead of copying over the whole file first you can just copy the segments you need. That being said, if you are going to stick with your original implementation, i think you just need to change AVURLAsset *assetUrl = [AVURLAsset assetWithURL:audioFileInput]; to AVURLAsset *assetUrl = [[AVURLAsset URLAssetWithURL:audioFileInput options:nil];
I Got sucess to save itune music in document libray by using following Method
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
self.fullPathToFileForAudio = [documentsDirectory stringByAppendingPathComponent:#"auto-old.m4a"];
NSFileManager *fileMgr = [NSFileManager defaultManager];
// get rid of existing mp4 if exists...
if ([fileMgr removeItemAtPath:self.fullPathToFileForAudio error:&error] != YES)
NSLog(#"Unable to delete file: %#", [error localizedDescription]);
[self convertVideoToLowQuailtyWithInputURL:self.musicUrl outputURL:[NSURL fileURLWithPath:self.fullPathToFileForAudio] handler:^(AVAssetExportSession *exportSession)
{
if (exportSession.status == AVAssetExportSessionStatusCompleted)
{
NSLog(#"completed %#",exportSession.error);
printf("completed\n");
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(#"%# PATH",self.fullPathToFileForAudio);
[self exporterCompleted:[NSURL fileURLWithPath:self.fullPathToFileForAudio]];
});
}
else
{
// NSLog(#"%#",exportSession.error);
printf("error\n");
dispatch_sync(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
});
}
}];
- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
handler:(void (^)(AVAssetExportSession*))handler
{
[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetAppleM4A];
CMTime startTime = CMTimeMake(minValue, 1);
CMTime stopTime = CMTimeMake(maxValue, 1);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);
exportSession.outputURL = outputURL;
exportSession.outputFileType = #"com.apple.m4a-audio";
exportSession.timeRange = exportTimeRange;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
{
handler(exportSession);
}];
}

Image not displayed on UICollectionViewCell

I am trying to get images on contacts,here i used UICollectionViewCell but in the collection view i didn't get image for the contact,i get only name and number.Here my code is
- (IBAction)ContactDisplay:(id)sender {
_addressBookController = [[ABPeoplePickerNavigationController alloc] init];
[_addressBookController setPeoplePickerDelegate:self];
[self presentViewController:_addressBookController animated:YES completion:nil];
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person
{
[self displayPerson:person];
}
- (void)displayPerson:(ABRecordRef)person
{
NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
NSLog(#"%#",name);
NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phone = #"[None]";
}
NSLog(#"%#",phone);
UIImage *img ;
if (person != nil && ABPersonHasImageData(person)) {
if ((&ABPersonCopyImageDataWithFormat) != nil ) {
img= [UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)];
}
} else {
NSString *imageUrlString = #"http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
NSURL *url = [NSURL URLWithString:imageUrlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
img= [UIImage imageWithData:data];
}
NSString *string ;//
string =[NSString stringWithFormat:#"%#",img];
NSLog(#"%#",img);
self.name.text=name;
self.number.text=phone;
[self.nameArray addObject:name];
[self.imageArray addObject:string];
NSLog(#"%#",self.nameArray);
NSLog(#"%#",self.imageArray);
[self.collectionView reloadData];
[self.collectionView performBatchUpdates:^{
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} completion:nil];
}
finally an image array i got like this
(
"add-button.png",
"<UIImage: 0x17e56c80>, {148, 148}"
)
On image array every image like display .PNG format it will display fine ,then how can modify it.
Can you please suggest me how can you solve this,thank you.
I don't fully agree with everything you're doing there but I think you're getting your data wrong. Try using this instead when you're fetching the ABPerson image data.
if (person != nil) {
CFDataRef imageData = ABPersonCopyImageData(person);
NSData *data = CFBridgingRelease(imageData);
if (data != nil && data.length > 10){ //arbitrary length to make sure our data object isnt' really empty
img = [UIImage imageWithData:data];
} else {
NSString *imageUrlString = #"http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
NSURL *url = [NSURL URLWithString:imageUrlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
img= [UIImage imageWithData:data];
}
Then don't store your images as Strings in your array. Store them either as NSData or UIImage, but NOT STRINGS.
so
[myArray addObject:img]; //not the string.
And when you fetch it later, make sure you treat is as an image and not as a string
on your storyboard, select the image and look at the properties panel.
there are "Installed" options at the bottom. check the topmost "Installed" box.
I think there might be issue with conversion of image to string
NSString *string ;//
string =[NSString stringWithFormat:#"%#",img];
Add image to image array without converting to string
[self.imageArray addObject:img];
I do it like this in my app. Assuming 'person' is an ABRecordRef.
NSMutableDictionary *contactInfoDict = [[NSMutableDictionary alloc]
initWithObjects:#[#"", #"", #"", #""]
forKeys:#[#"firstName", #"lastName", #"birthday", #"picture"]];
CFTypeRef generalCFObject;
// Firtname
generalCFObject = ABRecordCopyValue(person, kABPersonFirstNameProperty);
if (generalCFObject) {
[contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:#"firstName"];
CFRelease(generalCFObject);
}
// Lastname
generalCFObject = ABRecordCopyValue(person, kABPersonLastNameProperty);
if (generalCFObject) {
[contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:#"lastName"];
CFRelease(generalCFObject);
}
// Birthday
generalCFObject = ABRecordCopyValue(person, kABPersonBirthdayProperty);
if (generalCFObject) {
[contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:#"birthday"];
NSLog(#"Date : %#", [contactInfoDict objectForKey:#"birthday"]);
CFRelease(generalCFObject);
}
// User image
CFDataRef photo = ABPersonCopyImageData(person);
if (photo) {
CFRelease(photo);
UIImage *image = [UIImage imageWithData:(__bridge NSData*)photo];
[contactInfoDict setObject:image forKey:#"picture"];
}

Getting mp3 artwork crashes on iOS 8 but works on iOS 7

EDIT: The culprit was iOS 8, not the simulator (which I didn't realize was already running iOS 8) I've renamed the title to reflect this.
I was happily using the code from this SO question to load album artwork from mp3 files. This was on my iPhone 5 with iOS 7.1.
But then I traced crashing in the iOS simulator to this code. Further investigation revealed that this code also crashed on my iPad. It crashed on my iPad after upgrading it to iOS 8.
It appears the dictionary containing the image is corrupted.
I created a dummy iOS project that only loads album art and got the same result. Below is the code from that viewcontroller.
- (void) viewDidAppear:(BOOL)animated
{
self.titleText = #"Overkill"; // Set song filename here
NSString *filePath = [[NSBundle mainBundle] pathForResource:self.titleText ofType:#"mp3"];
if (!filePath) {
return;
}
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSLog(#"Getting song metadata for %#", self.titleText);
AVAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
if (asset != nil) {
NSArray *keys = [NSArray arrayWithObjects:#"commonMetadata", nil];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
withKey:AVMetadataCommonKeyArtwork
keySpace:AVMetadataKeySpaceCommon];
UIImage *albumArtWork;
for (AVMetadataItem *item in artworks) {
if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
NSDictionary *dict = [item.value copyWithZone:nil];
// **********
// Crashes here with SIGABRT. dict is not a valid dictionary.
// **********
if ([dict objectForKey:#"data"]) {
albumArtWork = [UIImage imageWithData:[dict objectForKey:#"data"]];
}
}
else if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
// This doesn't appear to get called for images set (ironically) in iTunes
albumArtWork = [UIImage imageWithData:[item.value copyWithZone:nil]];
}
}
if (albumArtWork != nil) {
dispatch_sync(dispatch_get_main_queue(), ^{
[self.albumArtImageView setImage:albumArtWork];
});
}
}];
}
}
I've marked the line with the crash. It expects a file Overkill.mp3 to be in the bundle. I tested with multiple mp3's and m4a's exported from iTunes and Amazon, so I know the files themselves are correctly encoded.
Tested in Xcode 6.0 and 6.1.
Any ideas why it would work on iPhone but not the simulator or iPad?
EDIT / UPDATE:
Logging the item.value reveals differences.
On iPhone 5 (works):
(lldb) po item.value
{
MIME = JPG;
data = <ffd8ffe0 .... several Kb of data ..... 2a17ffd9>;
identifier = "";
picturetype = Other;
}
On Simulator (crashes)
(lldb) po item.value
<ffd8ffe0 .... several Kb of data ..... 2a17ffd9>
So it appears that on the simulator there is no dictionary, just the raw artwork.
Changing the code to not expect a dictionary, but take item.value as a UIImage works!
for (AVMetadataItem *item in artworks) {
if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
NSData *newImage = [item.value copyWithZone:nil];
albumArtWork = [UIImage imageWithData:newImage];
}
...
}
It seems the returned data structure has changed in iOS 8. The value of the AVMetadataItem object is no longer a dictionary, but the actual raw UIImage data.
Adding a test for the NSFoundationVersionNumber solves the problem. There is probably a cleaner solution.
- (void) viewDidAppear:(BOOL)animated
{
self.titleText = #"Overkill";
NSString *filePath = [[NSBundle mainBundle] pathForResource:self.titleText ofType:#"mp3"];
if (!filePath) {
return;
}
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSLog(#"Getting song metadata for %#", self.titleText);
AVAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
if (asset != nil) {
NSArray *keys = [NSArray arrayWithObjects:#"commonMetadata", nil];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
withKey:AVMetadataCommonKeyArtwork
keySpace:AVMetadataKeySpaceCommon];
UIImage *albumArtWork;
for (AVMetadataItem *item in artworks) {
if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
// *** WE TEST THE IOS VERSION HERE ***
if (TARGET_OS_IPHONE && NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
NSData *newImage = [item.value copyWithZone:nil];
albumArtWork = [UIImage imageWithData:newImage];
}
else {
NSDictionary *dict = [item.value copyWithZone:nil];
if ([dict objectForKey:#"data"]) {
albumArtWork = [UIImage imageWithData:[dict objectForKey:#"data"]];
}
}
}
else if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
// This doesn't appear to get called for images set (ironically) in iTunes
albumArtWork = [UIImage imageWithData:[item.value copyWithZone:nil]];
}
}
if (albumArtWork != nil) {
dispatch_sync(dispatch_get_main_queue(), ^{
[self.albumArtImageView setImage:albumArtWork];
});
}
}];
}
}

-[__NSCFConstantString allKeys]: unrecognized selector sent to instance 0xf9ac8

My app when I run it on my phone I get this error:
I just tried to parse a json and trying to use images in two column of custom cell, but my images on scroll are mis-placed.
int index=indexPath.row*2;
int newindex=index+1;
NSDictionary *u = [[results objectAtIndex:index]mutableCopy ];
NSLog(#"NSDictionary *u = [results objectAtIndex:index] %#",u);
NSDictionary *u1 = [[results objectAtIndex:newindex]mutableCopy];
NSLog(#"NSDictionary *u1 = [results objectAtIndex:newindex] %#",u1);
But if i use both index and newindex same value it works.
:-
:-
Code :
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil suffix:(NSString *)_suffix{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.suffix = _suffix;
}
return self;
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)_tableView{
return 1;
}
//my new
- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section{
int resultCount = [results count];
labelResultsCount.text = [NSString stringWithFormat:#"%d",resultCount];
return resultCount/2;
}
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// NSString* CellIdentifier = [NSString stringWithFormat:#"ident_%d",indexPath.row];
static NSString *CellIdentifier = #"ResultCell";
ResultCell *cell = (ResultCell *)[_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
NSArray *a = [[NSBundle mainBundle] loadNibNamed:#"ResultCell" owner:self options:nil];
cell = (ResultCell *)[a objectAtIndex:0];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
int index=[(indexPath.row*2) copy];
int newindex=[(index+1) copy];
// save badge in dictionaory and get here and show in lable
NSDictionary *u = [[results objectAtIndex:index]mutableCopy ];
NSLog(#"NSDictionary *u = [results objectAtIndex:newindex] %#",u);
NSDictionary *u1 = [[results objectAtIndex:newindex]mutableCopy];
NSLog(#"NSDictionary *u1 = [results objectAtIndex:indexPath.row*2+1 newindex] %#",u1);
#try {
/* fullName */
NSString *nickName = [u objectForKey:#"nickName"];
cell.labelName.text = nickName;
NSString *nickName1 = [u1 objectForKey:#"nickName"];
cell.labelName1.text = nickName1;
/* chat notification*/
cell.imageViewNotification.hidden=0;
cell.imageViewNotification1.hidden=0;
NSString *badge = [u objectForKey:#"badge"];
NSString *badge1 = [u1 objectForKey:#"badge"];
if([badge intValue]>0)
{
cell.imageViewNotification.hidden=0;
cell.labelNotification.text = badge;
NSLog(#"inside > 0 %#",badge);
}
else if([badge intValue]<=0)
{
cell.imageViewNotification.hidden=1;
cell.labelNotification.text=#"";
NSLog(#"%#",badge);
}
if([badge1 intValue]>0)
{
cell.imageViewNotification1.hidden=0;
cell.labelNotification1.text = badge1;
NSLog(#"inside > 0 %#",badge1);
}
else if([badge1 intValue]<=0)
{
cell.imageViewNotification1.hidden=1;
cell.labelNotification1.text=#"";
NSLog(#"%#",badge1);
}
/*..................*/
/* distance */
id distance = [u objectForKey:#"distance"];
if([distance isKindOfClass:[NSString class]]){
cell.labelDistance.text = distance;
cell.imageViewDistance.hidden = 0;
}else{
cell.imageViewDistance.hidden = 1;
cell.labelDistance.text = #"";
}
id distance1 = [u1 objectForKey:#"distance"];
if([distance1 isKindOfClass:[NSString class]]){
cell.labelDistance1.text = distance1;
cell.imageViewDistance1.hidden = 0;
}else{
cell.imageViewDistance1.hidden = 1;
cell.labelDistance1.text = #"";
}
/* online */
NSNumber *online = [u objectForKey:#"online"];
cell.imageViewOnline.image = [online intValue] ? [UIImage imageNamed:#"circle_online.png"] : [UIImage imageNamed:#"circle_offline_red.png"];
NSNumber *online1 = [u1 objectForKey:#"online"];
cell.imageViewOnline1.image = [online1 intValue] ? [UIImage imageNamed:#"circle_online.png"] : [UIImage imageNamed:#"circle_offline_red.png"];
/* buttonProfile */
id d = [u objectForKey:#"thumbnails"];
id d1 = [u1 objectForKey:#"thumbnails"];
if([d isKindOfClass:[NSDictionary class]] ||[d1 isKindOfClass:[NSDictionary class]]){
if([[d allKeys] count]>0 ||[[d1 allKeys] count]>0){
NSString *imageSuffix = [d objectForKey:#"icon"];
NSString *imageSuffix1 = [d1 objectForKey:#"icon"];
NSLog(#"[d allKeys] count]%#", d);
NSLog(#"[d1 allKeys] count]%#", d1);
UIImage *image = [[SharingCenter sharedManager] imageFromCache:imageSuffix];
UIImage *image1 = [[SharingCenter sharedManager] imageFromCache:imageSuffix1];
if(image||image1)
{
[cell.buttonUserProfile setBackgroundImage:image forState:UIControlStateNormal];
[cell.buttonUserProfile1 setBackgroundImage:image1 forState:UIControlStateNormal];
}
else{
NSString *gender = [u objectForKey:#"gender"];
NSString *gender1 = [u1 objectForKey:#"gender"];
UIImage *profileDefualtImage = [gender isEqualToString:#"M"] ? [UIImage imageNamed:#"no_photo_male.png"] : [UIImage imageNamed:#"no_photo_female.png"];
UIImage *profileDefualtImage1 = [gender1 isEqualToString:#"M"] ? [UIImage imageNamed:#"no_photo_male.png"] : [UIImage imageNamed:#"no_photo_female.png"];
[cell.buttonUserProfile setBackgroundImage:profileDefualtImage forState:UIControlStateNormal];
[cell.buttonUserProfile1 setBackgroundImage:profileDefualtImage1 forState:UIControlStateNormal];
/* downlowd Image */
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#",WebServicePrefix,imageSuffix]];
__block
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request.delegate = self;
[request setPostValue:[NSString stringWithFormat:#"%f",[[SharingCenter sharedManager] currentCoordinate].latitude] forKey:#"lat"];
[request setPostValue:[NSString stringWithFormat:#"%f",[[SharingCenter sharedManager] currentCoordinate].longitude] forKey:#"lon"];
// [[[SharingCenter sharedManager] imagesCache] removeAllObjects];
[request setCompletionBlock:^{
NSData *imageData = [request responseData];
if(imageData){
UIImage *image = [UIImage imageWithData:imageData];
if(image){
[[[SharingCenter sharedManager] imagesCache] setObject:image forKey:imageSuffix];
if([[self.tableView indexPathsForVisibleRows] containsObject:indexPath]){
ResultCell *cell = (ResultCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell.buttonUserProfile setBackgroundImage:image forState:UIControlStateNormal];
}
}
}
}];
[request setFailedBlock:^{
NSError *error = request.error;
NSLog(#"%#",error);
}];
/* downlowd Image */
NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#",WebServicePrefix,imageSuffix1]];
__block
ASIFormDataRequest *request1 = [ASIFormDataRequest requestWithURL:url1];
request1.delegate = self;
[request1 setPostValue:[NSString stringWithFormat:#"%f",[[SharingCenter sharedManager] currentCoordinate].latitude] forKey:#"lat"];
[request1 setPostValue:[NSString stringWithFormat:#"%f",[[SharingCenter sharedManager] currentCoordinate].longitude] forKey:#"lon"];
// [[[SharingCenter sharedManager] imagesCache] removeAllObjects];
[request1 setCompletionBlock:^{
NSData *imageData1 = [request1 responseData];
if(imageData1){
UIImage *image1 = [UIImage imageWithData:imageData1];
if(image1){
[[[SharingCenter sharedManager] imagesCache] setObject:image1 forKey:imageSuffix1];
if([[self.tableView indexPathsForVisibleRows] containsObject:indexPath]){
ResultCell *cell = (ResultCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell.buttonUserProfile1 setBackgroundImage:image1 forState:UIControlStateNormal];
}
}
}
}];
[request1 setFailedBlock:^{
NSError *error = request1.error;
NSLog(#"%#",error);
}];
[[self networkQueue] addOperation:request];
[[self networkQueue] addOperation:request1];
}
}
}
else{
NSString *gender = [u objectForKey:#"gender"];
NSString *gender1 = [u1 objectForKey:#"gender"];
UIImage *profileDefualtImage = [gender isEqualToString:#"M"] ? [UIImage imageNamed:#"no_photo_male.png"] : [UIImage imageNamed:#"no_photo_female.png"];
[cell.buttonUserProfile setBackgroundImage:profileDefualtImage forState:UIControlStateNormal];
UIImage *profileDefualtImage1 = [gender1 isEqualToString:#"M"] ? [UIImage imageNamed:#"no_photo_male.png"] : [UIImage imageNamed:#"no_photo_female.png"];
[cell.buttonUserProfile1 setBackgroundImage:profileDefualtImage1 forState:UIControlStateNormal];
}
/* pickStatus */
NSNumber *pickStatus = [u objectForKey:#"pick_status"];
NSNumber *pickStatus1 = [u1 objectForKey:#"pick_status"];
switch ([pickStatus intValue]) {
case 0:
cell.imageViewBorder.image = [UIImage imageNamed:#"border_yellow_transperent.png"];
cell.buttonUserPick.enabled = 1;
[cell.buttonUserPick setBackgroundImage:[UIImage imageNamed:#"button_circle_pick.png"] forState:UIControlStateNormal];
break;
case 1:
cell.imageViewBorder.image = [UIImage imageNamed:#"border_red_transperent.png"];
cell.buttonUserPick.enabled = 0;
[cell.buttonUserPick setBackgroundImage:[UIImage imageNamed:#"button_circle_wait.png"] forState:UIControlStateDisabled];
break;
case 2:
cell.imageViewBorder.image = [UIImage imageNamed:#"border_red_transperent.png"];
cell.buttonUserPick.enabled = 1;
[cell.buttonUserPick setBackgroundImage:[UIImage imageNamed:#"button_circle_pick.png"] forState:UIControlStateNormal];
break;
case 3:
cell.imageViewBorder.image = [UIImage imageNamed:#"border_red_transperent.png"];
cell.buttonUserPick.enabled = 0;
[cell.buttonUserPick setBackgroundImage:[UIImage imageNamed:#"button_circle_date.png"] forState:UIControlStateDisabled];
break;
default:
break;
}
switch ([pickStatus1 intValue]) {
case 0:
cell.imageViewBorder1.image = [UIImage imageNamed:#"border_yellow_transperent.png"];
cell.buttonUserPick1.enabled = 1;
[cell.buttonUserPick1 setBackgroundImage:[UIImage imageNamed:#"button_circle_pick.png"] forState:UIControlStateNormal];
break;
case 1:
cell.imageViewBorder1.image = [UIImage imageNamed:#"border_red_transperent.png"];
cell.buttonUserPick1.enabled = 0;
[cell.buttonUserPick1 setBackgroundImage:[UIImage imageNamed:#"button_circle_wait.png"] forState:UIControlStateDisabled];
break;
case 2:
cell.imageViewBorder1.image = [UIImage imageNamed:#"border_red_transperent.png"];
cell.buttonUserPick1.enabled = 1;
[cell.buttonUserPick1 setBackgroundImage:[UIImage imageNamed:#"button_circle_pick.png"] forState:UIControlStateNormal];
break;
case 3:
cell.imageViewBorder1.image = [UIImage imageNamed:#"border_red_transperent.png"];
cell.buttonUserPick1.enabled = 0;
[cell.buttonUserPick1 setBackgroundImage:[UIImage imageNamed:#"button_circle_date.png"] forState:UIControlStateDisabled];
break;
default:
break;
}
}
#catch (NSException * e) {
NSLog(#"%#",e);
}
#finally {
return cell;
}
}
Educated guess:
This line causes your problem:
if([d isKindOfClass:[NSDictionary class]] ||[d1 isKindOfClass:[NSDictionary class]]){
if([[d allKeys] count]>0 ||[[d1 allKeys] count]>0){
There are 4 different possibilities how the first if will come out:
d and d1 are both NSStrings. Line 2 will not be called
d is NSDictionary, d1 is a NSString. Line 2 will be called
d is NSString, d1 is a NSDictionary. Line 2 will be called
d and d1 are both NSDictionaries. Line 2 will be called.
First case is no problem at all. Last case neither. In case 2 and 3 allKeys will be called on a object that is not a NSDictionary.
You should probably replace it with an "if" that needs both tests to be true.
if([d isKindOfClass:[NSDictionary class]] && [d1 isKindOfClass:[NSDictionary class]]){
^^
Another option would be to check individually.
if([d isKindOfClass:[NSDictionary class]]) {
x = [d allKeys];
}
if([d1 isKindOfClass:[NSDictionary class]]) {
x = [d1 allKeys];
}
You are probably doing something to those NSDictionary objects somewhere, reading their allKeys. But my guess is that the objects you try to fetch from that results array aren't NSDictionary objects but NSString objects.
Try logging their class property to see what you are dealing with (you can also find this out by just looking at the JSON of course).
NSDictionary *u = [[results objectAtIndex:index]mutableCopy ];
NSLog(#"u Class: %#", [u class]);
Also, why are you making a mutableCopy of the object right to a non-mutable NSDictionary object.
Using the following should do the trick normally:
NSDictionary* u = [results objectAtIndex:index];
NSLog(#"u Class: %#", [u class]);
That's a lot of code to look through, but I think this could be the problem:
if([d isKindOfClass:[NSDictionary class]] ||[d1 isKindOfClass:[NSDictionary class]]){
if([[d allKeys] count]>0 ||[[d1 allKeys] count]>0){
This if statement will pass if either d or d1 is a dictionary. If one isn't, it would cause that error you see on the second line. Log the class of d and d1 to see if they're both dictionaries.