I'm trying to get artwork of the mp3 file using these lines of code.
CFDataRef AlbumPic= nil;
UInt32 picDataSize = sizeof(picDataSize);
err =AudioFileGetGlobalInfo(kAudioFilePropertyAlbumArtwork, 0, NULL, &picDataSize, &AlbumPic);
if( err != noErr ) {
NSLog( #"error" );
}
NSData* imagedata= (__bridge NSData*)AlbumPic;
UIImage* image=[[UIImage alloc]initWithData:imagedata];
If I'm using:
NSLog (#"image %#, image");
or
NSLog (#"image %#, [image description]");
there is NULL.
How can I get artwork of mp3 file using kAudioFilePropertyAlbumArtwork?
The function you want is AudioFileGetProperty, not AudioFileGetGlobalInfo
Related
I am developing application which changes the contact picture of the contact when i select any contact from the contact picker .
Here is the code I have implemented so far,
(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
CFErrorRef error=nil;
ABAddressBookRef addressBook = ABAddressBookCreate();
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"birthday.jpg" ofType:nil];
UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
if(ABPersonHasImageData(person))
{
ABPersonRemoveImageData(person, &error);
ABAddressBookSave(addressBook, &error);
}
if (ABPersonSetImageData(person,cfDataRef, &error))
{
NSLog(#"Set contact photo %#", error);
if (ABAddressBookHasUnsavedChanges(addressBook))
{
NSLog(#"Changes made to address book");
}
else {
NSLog(#"No changes made to address book");
}
if (ABAddressBookSave(addressBook, &error))
{
NSLog(#"Saved");
}
else {
NSLog(#"Not saved");
}
}
CFRelease(cfDataRef);
// TODO: release peoplePicker (and refactor code to not have it global)
[self dismissModalViewControllerAnimated:YES];
return NO;
}
But when I debug and look at ABAddressBookHasUnsavedChanges it is returning false and image is not getting set to the contact. What might be the reason?
I wrote this method in shouldContinueAfterSelectingPerson; I have checked the image and it's not null and ABPersonSetImageData is returning TRUE.
I have updated the code , checked for the image set already , if yes then i am removing that image and saving the saving the address book and relased CFDataRef.
But still its not working
Update:2
I have edited the code , please look at the changes.
I have written all my code in this method
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { } ,
So definately there will be person who is already in address book . But i tried according to above code still that ABAddressBookHasUnsavedChanges returning false
You are creating the data wrong - you should be doing it like this.
UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
Also try setting a break point somewhere in the beginning and check if something isn't nil or doesn't have wrong value.
UPDATE
I have tried it in actual Xcode, just to test. The reason why your code tells you that address book has no unsaved changes is because you haven't added the record inside it yet - otherwise you are setting an image on a contact that hasn't been added to address book yet (that is not a problem, you can do that without any problems, but the contact itself is not in the address book until you save it). So the magical line of code for you will probably be this:
ABAddressBookAddRecord(addressBook, person, &error);
This is the full code I've been using.
CFErrorRef error = nil;
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"sky_main" ofType:#"jpg"];
UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
if (ABPersonHasImageData(person)) {
ABPersonRemoveImageData(person, &error);
ABAddressBookSave(addressBook, &error);
}
ABAddressBookAddRecord(addressBook, person, &error);
if (ABPersonSetImageData(person, cfDataRef, &error)) {
if (ABAddressBookHasUnsavedChanges(addressBook)) {
NSLog(#"has unsaved changes");
} else {
NSLog(#"nothing to save");
}
if (ABAddressBookSave(addressBook, &error)) {
NSLog(#"saved");
} else {
NSLog(#"not saved");
}
}
ABUnknownPersonViewController *ctr = [[ABUnknownPersonViewController alloc] init];
ctr.unknownPersonViewDelegate = nil;
ctr.displayedPerson = person;
ctr.allowsAddingToAddressBook = YES;
ctr.allowsActions = YES;
ctr.hidesBottomBarWhenPushed = YES;
// [[[CCDirector sharedDirector] view] addSubview:ctr.view];
CFRelease(person);
CFRelease(addressBook);
I am also using the AddressBookUI, but you don't have to use it (I just wanted to see the changes to the unsaved contact).
UPDATE 2
Ok, just created a new project just to try. It looks like the problem was actually with the ABAddressBookRef. You have to get that from the peoplePicker argument of the delegate method. Since it is the delegate method you don't have to add the record because it already exists. This is the code that has to work (tried it 2 minutes ago). Let me know :)
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
CFErrorRef error = nil;
ABAddressBookRef addressBook = peoplePicker.addressBook;
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"cat" ofType:#"jpg"];
UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
if (ABPersonHasImageData(person)) {
ABPersonRemoveImageData(person, &error);
ABAddressBookSave(addressBook, &error);
}
if (ABPersonSetImageData(person, cfDataRef, &error)) {
if (ABAddressBookHasUnsavedChanges(addressBook)) {
NSLog(#"has unsaved changes");
} else {
NSLog(#"nothing to save");
}
if (ABAddressBookSave(addressBook, &error)) {
NSLog(#"saved");
} else {
NSLog(#"not saved");
}
}
CFRelease(addressBook);
return YES;
}
Btw., since it is the delegate method do not release the person otherwise it will crash horribly - just kidding, it will crash, because it tries to access memory that doesn't exist.
So I want to make my app edit and save ID3 tags. I found on some site a long time ago on how to read them, here is this code in my app:
-(NSDictionary*)MP3InfoDictionary {
AudioFileID fileID = nil;
OSStatus err = noErr;
err = AudioFileOpenURL( (CFURLRef) self, kAudioFileReadPermission, 0, &fileID);
if (err != noErr) {
//NSLog(#"AudioFileOpenURL failed");
}
UInt32 id3DataSize = 0;
char * rawID3Tag = NULL;
err = AudioFileGetPropertyInfo(fileID, kAudioFilePropertyID3Tag, &id3DataSize, NULL);
if (err != noErr) {
NSLog(#"AudioFileGetPropeetyInfo failed for ID3 tag");
}
//NSLog(#"id3 data size is %lu bytes",id3DataSize);
rawID3Tag = (char *) malloc(id3DataSize);
if (rawID3Tag == NULL) {
//NSLog(#"could not allocated %lu bytes of memory for ID3 tag", id3DataSize);
}
err = AudioFileGetProperty(fileID, kAudioFilePropertyID3Tag, &id3DataSize, rawID3Tag);
if (err != noErr) {
NSLog(#"AudioFileGetProperty failed for ID3 tag");
}
//NSLog(#"read %lu bytes of ID3 info", id3DataSize);
CFDictionaryRef piDict = nil;
UInt32 piDataSize = sizeof(piDict);
free(rawID3Tag);
err = AudioFileGetProperty(fileID, kAudioFilePropertyInfoDictionary, &piDataSize, &piDict);
if (err != noErr) {
//NSLog(#"AudioFileGetProperty failed for property info dictionary");
}
//NSLog(#"Property info:%#", (NSDictionary*)piDict);
//CFShow(piDict);
NSDictionary *MP3InfoDictionary = (NSDictionary*)piDict;
if (MP3InfoDictionary != NULL) {
return MP3InfoDictionary;
}
return nil;
}
This code is in a NSURL category, the url is a file url of a mp3 in the documents directory of the iOS. It works 100%, I get all the titles, artwork, lyrics, etc. But now I want to save and overwrite values of the mp3 like artist, title, etc. On another post on here a guy posted that you use AudioFileSetProperty(), how do I use it correctly?
Apparently it's because of the kAudioFileReadPermission but It tells me that the mp3 is only readable, but that can't be true because another app I have can change ID3 tags and can on the same mp3 I've tried to edit.
EDIT:
See my first comment below. It appears AudioFile does not allow modifying the four types of files listed in the link.
i am developing an audio player. I successfully get other information of the mp3 file. but is unable to get the album art of the mp3 file. using this code i get the mp3 file info.
- (NSDictionary *)songID3Tags
{
AudioFileID fileID = nil;
OSStatus error = noErr;
error = AudioFileOpenURL((CFURLRef)self.filePath, kAudioFileReadPermission, 0, &fileID);
if (error != noErr) {
NSLog(#"AudioFileOpenURL failed");
}
UInt32 id3DataSize = 0;
char *rawID3Tag = NULL;
error = AudioFileGetPropertyInfo(fileID, kAudioFilePropertyID3Tag, &id3DataSize, NULL);
if (error != noErr)
NSLog(#"AudioFileGetPropertyInfo failed for ID3 tag");
rawID3Tag = (char *)malloc(id3DataSize);
if (rawID3Tag == NULL)
NSLog(#"could not allocate %lu bytes of memory for ID3 tag", id3DataSize);
error = AudioFileGetProperty(fileID, kAudioFilePropertyID3Tag, &id3DataSize, rawID3Tag);
if( error != noErr )
NSLog(#"AudioFileGetPropertyID3Tag failed");
UInt32 id3TagSize = 0;
UInt32 id3TagSizeLength = 0;
error = AudioFormatGetProperty(kAudioFormatProperty_ID3TagSize, id3DataSize, rawID3Tag, &id3TagSizeLength, &id3TagSize);
if (error != noErr) {
NSLog( #"AudioFormatGetProperty_ID3TagSize failed" );
switch(error) {
case kAudioFormatUnspecifiedError:
NSLog( #"Error: audio format unspecified error" );
break;
case kAudioFormatUnsupportedPropertyError:
NSLog( #"Error: audio format unsupported property error" );
break;
case kAudioFormatBadPropertySizeError:
NSLog( #"Error: audio format bad property size error" );
break;
case kAudioFormatBadSpecifierSizeError:
NSLog( #"Error: audio format bad specifier size error" );
break;
case kAudioFormatUnsupportedDataFormatError:
NSLog( #"Error: audio format unsupported data format error" );
break;
case kAudioFormatUnknownFormatError:
NSLog( #"Error: audio format unknown format error" );
break;
default:
NSLog( #"Error: unknown audio format error" );
break;
}
}
CFDictionaryRef piDict = nil;
UInt32 piDataSize = sizeof(piDict);
error = AudioFileGetProperty(fileID, kAudioFilePropertyInfoDictionary, &piDataSize, &piDict);
if (error != noErr)
NSLog(#"AudioFileGetProperty failed for property info dictionary");
free(rawID3Tag);
return (NSDictionary*)piDict;
}
I know through kAudioFilePropertyAlbumArtwork i can get the album art of the mp3 file, but I do not know how to get it.
- (NSArray *)artworksForFileAtPath:(NSString *)path {
NSMutableArray *artworkImages = [NSMutableArray array];
NSURL *u = [NSURL fileURLWithPath:path];
AVURLAsset *a = [AVURLAsset URLAssetWithURL:u options:nil];
NSArray *artworks = [AVMetadataItem metadataItemsFromArray:a.commonMetadata withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon];
for (AVMetadataItem *i in artworks)
{
NSString *keySpace = i.keySpace;
UIImage *im = nil;
if ([keySpace isEqualToString:AVMetadataKeySpaceID3])
{
NSDictionary *d = [i.value copyWithZone:nil];
im = [UIImage imageWithData:[d objectForKey:#"data"]];
}
else if ([keySpace isEqualToString:AVMetadataKeySpaceiTunes])
im = [UIImage imageWithData:[i.value copyWithZone:nil]];
if (im)
[artworkImages addObject:im];
}
NSLog(#"array description is %#", [artworkImages description]);
return artworkImages; }
The Above code return the album_art of the mp3 file. Where path is the audio file path.
This question already has answers here:
UIImagePickerController and extracting EXIF data from existing photos
(18 answers)
Closed 7 years ago.
How can we get Exif information from UIImage selected from UIImagePickerController?
I had done much R&D for this and got many replies but still failed to implement this.
I had gone through this this and this link
Please help me to solve this problem.
Thanks in advance..
Interesting question! I came up with the following solution working for images picked from your photo library (note my code is using ARC):
Import AssetsLibrary.framework and ImageIO.framework.
Then include the needed classes inside your .h-file:
#import <AssetsLibrary/ALAsset.h>
#import <AssetsLibrary/ALAssetRepresentation.h>
#import <ImageIO/CGImageSource.h>
#import <ImageIO/CGImageProperties.h>
And put this inside your imagePickerController:didFinishPickingMediaWithInfo: delegate method:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL]
resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *image_representation = [asset defaultRepresentation];
// create a buffer to hold image data
uint8_t *buffer = (Byte*)malloc(image_representation.size);
NSUInteger length = [image_representation getBytes:buffer fromOffset: 0.0 length:image_representation.size error:nil];
if (length != 0) {
// buffer -> NSData object; free buffer afterwards
NSData *adata = [[NSData alloc] initWithBytesNoCopy:buffer length:image_representation.size freeWhenDone:YES];
// identify image type (jpeg, png, RAW file, ...) using UTI hint
NSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys:(id)[image_representation UTI] ,kCGImageSourceTypeIdentifierHint,nil];
// create CGImageSource with NSData
CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef) adata, (__bridge CFDictionaryRef) sourceOptionsDict);
// get imagePropertiesDictionary
CFDictionaryRef imagePropertiesDictionary;
imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(sourceRef,0, NULL);
// get exif data
CFDictionaryRef exif = (CFDictionaryRef)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyExifDictionary);
NSDictionary *exif_dict = (__bridge NSDictionary*)exif;
NSLog(#"exif_dict: %#",exif_dict);
// save image WITH meta data
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *fileURL = nil;
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, imagePropertiesDictionary);
if (![[sourceOptionsDict objectForKey:#"kCGImageSourceTypeIdentifierHint"] isEqualToString:#"public.tiff"])
{
fileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#.%#",
documentsDirectory,
#"myimage",
[[[sourceOptionsDict objectForKey:#"kCGImageSourceTypeIdentifierHint"] componentsSeparatedByString:#"."] objectAtIndex:1]
]];
CGImageDestinationRef dr = CGImageDestinationCreateWithURL ((__bridge CFURLRef)fileURL,
(__bridge CFStringRef)[sourceOptionsDict objectForKey:#"kCGImageSourceTypeIdentifierHint"],
1,
NULL
);
CGImageDestinationAddImage(dr, imageRef, imagePropertiesDictionary);
CGImageDestinationFinalize(dr);
CFRelease(dr);
}
else
{
NSLog(#"no valid kCGImageSourceTypeIdentifierHint found …");
}
// clean up
CFRelease(imageRef);
CFRelease(imagePropertiesDictionary);
CFRelease(sourceRef);
}
else {
NSLog(#"image_representation buffer length == 0");
}
}
failureBlock:^(NSError *error) {
NSLog(#"couldn't get asset: %#", error);
}
];
One thing I noticed is, that iOS will ask the user to allow location services – if he denies, you won't be abled to get the image data …
EDIT
Added code to save the image including its meta data. It's a quick approach, so maybe there is a better way, but it works!
These answers all seem extremely complex. If the image has been saved to the Camera Roll, and you have the ALAsset (either from UIImagePicker or ALAssetLibrary) you can get the metadata like so:
asset.defaultRepresentation.metadata;
If you want to save that image from camera roll to another location (say in Sandbox/Documents) simply do:
CGImageDestinationRef imageDestinationRef = CGImageDestinationCreateWithURL((__bridge CFURLRef)urlToSaveTo, kUTTypeJPEG, 1, NULL);
CFDictionaryRef imagePropertiesRef = (__bridge CFDictionaryRef)asset.defaultRepresentation.metadata;
CGImageDestinationAddImage(imageDestinationRef, asset.defaultRepresentation.fullResolutionImage, imagePropertiesRef);
if (!CGImageDestinationFinalize(imageDestinationRef)) NSLog(#"Failed to copy photo on save to %#", urlToSaveTo);
CFRelease(imageDestinationRef);
I had found solution and got answer from here
From here We can get GPS info as well..
Amazing and thanks all for helping me to solve this problem.
UPDATE
This is another function that I had created myself, also return Exif data as well as GPS data and in this function we doesn't need any third party library.. but you have to turn on location services for this. and use current latitude and longitude for that. so have to use CoreLocation.framework
//FOR CAMERA IMAGE
-(NSMutableData *)getImageWithMetaData:(UIImage *)pImage
{
NSData* pngData = UIImagePNGRepresentation(pImage);
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)pngData, NULL);
NSDictionary *metadata = (NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
NSMutableDictionary *metadataAsMutable = [[metadata mutableCopy]autorelease];
[metadata release];
//For GPS Dictionary
NSMutableDictionary *GPSDictionary = [[[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGPSDictionary]mutableCopy]autorelease];
if(!GPSDictionary)
GPSDictionary = [NSMutableDictionary dictionary];
[GPSDictionary setValue:[NSNumber numberWithDouble:currentLatitude] forKey:(NSString*)kCGImagePropertyGPSLatitude];
[GPSDictionary setValue:[NSNumber numberWithDouble:currentLongitude] forKey:(NSString*)kCGImagePropertyGPSLongitude];
NSString* ref;
if (currentLatitude <0.0)
ref = #"S";
else
ref =#"N";
[GPSDictionary setValue:ref forKey:(NSString*)kCGImagePropertyGPSLatitudeRef];
if (currentLongitude <0.0)
ref = #"W";
else
ref =#"E";
[GPSDictionary setValue:ref forKey:(NSString*)kCGImagePropertyGPSLongitudeRef];
[GPSDictionary setValue:[NSNumber numberWithFloat:location.altitude] forKey:(NSString*)kCGImagePropertyGPSAltitude];
//For EXIF Dictionary
NSMutableDictionary *EXIFDictionary = [[[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyExifDictionary]mutableCopy]autorelease];
if(!EXIFDictionary)
EXIFDictionary = [NSMutableDictionary dictionary];
[EXIFDictionary setObject:[NSDate date] forKey:(NSString*)kCGImagePropertyExifDateTimeOriginal];
[EXIFDictionary setObject:[NSDate date] forKey:(NSString*)kCGImagePropertyExifDateTimeDigitized];
//add our modified EXIF data back into the image’s metadata
[metadataAsMutable setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];
[metadataAsMutable setObject:GPSDictionary forKey:(NSString *)kCGImagePropertyGPSDictionary];
CFStringRef UTI = CGImageSourceGetType(source);
NSMutableData *dest_data = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)dest_data, UTI, 1, NULL);
if(!destination)
dest_data = [[pngData mutableCopy] autorelease];
else
{
CGImageDestinationAddImageFromSource(destination, source, 0, (CFDictionaryRef) metadataAsMutable);
BOOL success = CGImageDestinationFinalize(destination);
if(!success)
dest_data = [[pngData mutableCopy] autorelease];
}
if(destination)
CFRelease(destination);
CFRelease(source);
return dest_data;
}
//FOR PHOTO LIBRARY IMAGE
-(NSMutableData *)getImagedataPhotoLibrary:(NSDictionary *)pImgDictionary andImage:(UIImage *)pImage
{
NSData* data = UIImagePNGRepresentation(pImage);
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)data, NULL);
NSMutableDictionary *metadataAsMutable = [[pImgDictionary mutableCopy]autorelease];
CFStringRef UTI = CGImageSourceGetType(source);
NSMutableData *dest_data = [NSMutableData data];
//For Mutabledata
CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)dest_data, UTI, 1, NULL);
if(!destination)
dest_data = [[data mutableCopy] autorelease];
else
{
CGImageDestinationAddImageFromSource(destination, source, 0, (CFDictionaryRef) metadataAsMutable);
BOOL success = CGImageDestinationFinalize(destination);
if(!success)
dest_data = [[data mutableCopy] autorelease];
}
if(destination)
CFRelease(destination);
CFRelease(source);
return dest_data;
}
and We will retrieve that data like this
//FOR CAMERA IMAGE
NSData *originalImgData = [self getImageWithMetaData:imgOriginal];
//FOR PHOTO LIBRARY IMAGE
[self getImagedataPhotoLibrary:[[myasset defaultRepresentation] metadata] andImage:imgOriginal];
For all of this you should have to Import AssetsLibrary.framework and ImageIO.framework.
I have used this method for getting the exifdata dictionary from image , I hope this will also work for you
-(void)getExifDataFromImage:(UIImage *)currentImage
{
NSData* pngData = UIImageJPEGRepresentation(currentImage, 1.0);
CGImageSourceRef mySourceRef = CGImageSourceCreateWithData((CFDataRef)pngData, NULL);
//CGImageSourceRef mySourceRef = CGImageSourceCreateWithURL((__bridge CFURLRef)myURL, NULL);
if (mySourceRef != NULL)
{
NSDictionary *myMetadata = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex(mySourceRef,0,NULL);
NSDictionary *exifDic = [myMetadata objectForKey:(NSString *)kCGImagePropertyExifDictionary];
NSDictionary *tiffDic = [myMetadata objectForKey:(NSString *)kCGImagePropertyTIFFDictionary];
NSLog(#"exifDic properties: %#", myMetadata); //all data
float rawShutterSpeed = [[exifDic objectForKey:(NSString *)kCGImagePropertyExifExposureTime] floatValue];
int decShutterSpeed = (1 / rawShutterSpeed);
NSLog(#"Camera %#",[tiffDic objectForKey:(NSString *)kCGImagePropertyTIFFModel]);
NSLog(#"Focal Length %#mm",[exifDic objectForKey:(NSString *)kCGImagePropertyExifFocalLength]);
NSLog(#"Shutter Speed %#", [NSString stringWithFormat:#"1/%d", decShutterSpeed]);
NSLog(#"Aperture f/%#",[exifDic objectForKey:(NSString *)kCGImagePropertyExifFNumber]);
NSNumber *ExifISOSpeed = [[exifDic objectForKey:(NSString*)kCGImagePropertyExifISOSpeedRatings] objectAtIndex:0];
NSLog(#"ISO %ld",[ExifISOSpeed integerValue]);
NSLog(#"Taken %#",[exifDic objectForKey:(NSString*)kCGImagePropertyExifDateTimeDigitized]);
}
}
You need ALAssetsLibrary to actually retrieve the EXIF info from an image. The EXIF is added to an image only when it is saved to the Photo Library. Even if you use ALAssetLibrary to get an image asset from the library, it will lose all EXIF info if you set it to a UIImage.
I have tried to insert GPS coordinates into image metadata picked by iPad Camera as it was suggested by Mehul.
It Works, Thank you for your post.
P.S.
Who intends to use that code, just substitude the two geolocations at the top of the function -(NSMutableData *)getImageWithMetaData:(UIImage *)pImage {
double currentLatitude = [locationManager location].coordinate.latitude;
double currentLongitude = [locationManager location].coordinate.longitude;
...
By supposing that you have already initializied somewhere locationManager in your code, like this:
locationManager = [[CLLocationManager alloc] init];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDelegate:self]; // Not necessary in this case
[locationManager startUpdatingLocation]; // Not neccessary in this case
and by importing CoreLocation/CoreLocation.h and ImageIO/ImageIO.h headers with associated frameworks.
I try using my app ALAssetRepresentation.and when i loop om an images there are couple of image that crash the app
for(ALAsset *asset in _assets) {
NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
[workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:#"UIImagePickerControllerMediaType"];
ALAssetRepresentation *representation = [asset defaultRepresentation];
if (!representation) {
[workingDictionary release];
continue;
}
CGImageRef imageRef = [representation fullResolutionImage];//here the app crash
UIImage *img = [UIImage imageWithCGImage:imageRef];
if (!img) {
[workingDictionary release];
continue;
}
if (!img) {
[workingDictionary release];
continue;
}
[workingDictionary setObject:img forKey:#"UIImagePickerControllerOriginalImage"];
[workingDictionary setObject:[asset valueForProperty:ALAssetPropertyOrientation] forKey:#"orientation"];
[returnArray addObject:workingDictionary];
[workingDictionary release];
}
in this line i get crash without any msg:
CGImageRef imageRef = [representation fullResolutionImage];
This is the crash msg
Program received signal: “0”.
Data Formatters temporarily unavailable, will re-try after a 'continue'. (Unknown error loading shared library "/Developer/usr/lib/libXcodeDebuggerSupport.dylib")
That is most likely due to running out of memory, how big are the images that cause the crash?
I had a similar problem and after hours of lookin for solution I found this - the best solution of too big Asset bug:
// For details, see http://mindsea.com/2012/12/18/downscaling-huge-alassets-without-fear-of-sigkill
#import <AssetsLibrary/AssetsLibrary.h>
#import <ImageIO/ImageIO.h>
// Helper methods for thumbnailForAsset:maxPixelSize:
static size_t getAssetBytesCallback(void *info, void *buffer, off_t position, size_t count) {
ALAssetRepresentation *rep = (__bridge id)info;
NSError *error = nil;
size_t countRead = [rep getBytes:(uint8_t *)buffer fromOffset:position length:count error:&error];
if (countRead == 0 && error) {
// We have no way of passing this info back to the caller, so we log it, at least.
NSLog(#"thumbnailForAsset:maxPixelSize: got an error reading an asset: %#", error);
}
return countRead;
}
static void releaseAssetCallback(void *info) {
// The info here is an ALAssetRepresentation which we CFRetain in thumbnailForAsset:maxPixelSize:.
// This release balances that retain.
CFRelease(info);
}
// Returns a UIImage for the given asset, with size length at most the passed size.
// The resulting UIImage will be already rotated to UIImageOrientationUp, so its CGImageRef
// can be used directly without additional rotation handling.
// This is done synchronously, so you should call this method on a background queue/thread.
- (UIImage *)thumbnailForAsset:(ALAsset *)asset maxPixelSize:(NSUInteger)size {
NSParameterAssert(asset != nil);
NSParameterAssert(size > 0);
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGDataProviderDirectCallbacks callbacks = {
.version = 0,
.getBytePointer = NULL,
.releaseBytePointer = NULL,
.getBytesAtPosition = getAssetBytesCallback,
.releaseInfo = releaseAssetCallback,
};
CGDataProviderRef provider = CGDataProviderCreateDirect((void *)CFBridgingRetain(rep), [rep size], &callbacks);
CGImageSourceRef source = CGImageSourceCreateWithDataProvider(provider, NULL);
CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(source, 0, (__bridge CFDictionaryRef) #{
(NSString *)kCGImageSourceCreateThumbnailFromImageAlways : #YES,
(NSString *)kCGImageSourceThumbnailMaxPixelSize : [NSNumber numberWithInt:size],
(NSString *)kCGImageSourceCreateThumbnailWithTransform : #YES,
});
CFRelease(source);
CFRelease(provider);
if (!imageRef) {
return nil;
}
UIImage *toReturn = [UIImage imageWithCGImage:imageRef];
CFRelease(imageRef);
return toReturn;
}