Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get a photo's original filename in iOS?
    primarykey
    data
    text
    <p>I'm currently developing an iPad app where a user will enter a photo filename in a text field (as part of field notes), then later they will import their photos to the iPad's photo library. The app will access the library using ALAssetsLibrary and enumerate over the photos, looking for ones with the filename they entered in their field notes. This would be the filename given to the photo by the camera that took it. For example "DSC_0019.JPG". </p> <p>Is this not possible?</p> <p>I noticed that if I import photos from my camera to iPad, then open iPhoto on my Mac and look at the iPad as a camera, I can "get info" on the images held on the iPad and see the original filename I'm looking for. However this is not contained in the metadata on the iPad.</p> <p>Any help would be greatly appreciated. </p> <p>Here is my code:</p> <p>(In working with the CFDictionary, pretty much everything is null except the Exif keys which don't have what i'm looking for)</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; //start activity animation [self.activity setHidden:NO]; [self.activity startAnimating]; //init our arrays autoAssignedAssets = [[NSMutableArray alloc] init]; unAssignedRecords = [[NSMutableArray alloc] init]; unAssignedAssets = [[NSMutableArray alloc] init]; //setup the library ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; //[ BLOCK ] =&gt; assetEnumerator // void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) { if (result != nil) { if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto] ) { //================================================================= ALAssetRepresentation* representation = [result defaultRepresentation]; // create a buffer to hold the data for the asset's image uint8_t *buffer = (Byte*)malloc(representation.size);// copy the data from the asset into the buffer NSUInteger length = [representation getBytes:buffer fromOffset: 0.0 length:representation.size error:nil]; // convert the buffer into a NSData object, free the buffer after NSData *adata = [[NSData alloc] initWithBytesNoCopy:buffer length:representation.size freeWhenDone:YES]; // setup a dictionary with a UTI hint. The UTI hint identifies the type of image we are dealing with (ie. a jpeg, png, or a possible RAW file) // specify the source hint NSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys: (id)[representation UTI] ,kCGImageSourceTypeIdentifierHint, nil]; // create a CGImageSource with the NSData. A image source can contain x number of thumbnails and full images. CGImageSourceRef sourceRef = CGImageSourceCreateWithData((CFDataRef) adata, (CFDictionaryRef) sourceOptionsDict); [adata release]; CFDictionaryRef imagePropertiesDictionary; // get a copy of the image properties from the CGImageSourceRef imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(sourceRef,0, NULL); //NSString *imageFilename = (NSString*)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyCIFFImageFileName); NSLog(@"%@", (NSDictionary *)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyExifDictionary)); CFNumberRef imageWidth = (CFNumberRef)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyPixelWidth); CFNumberRef imageHeight = (CFNumberRef)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyPixelHeight); int w = 0; int h = 0; CFNumberGetValue(imageWidth, kCFNumberIntType, &amp;w); CFNumberGetValue(imageHeight, kCFNumberIntType, &amp;h); // cleanup memory CFRelease(imagePropertiesDictionary); CFRelease(sourceRef); //NSLog(@"width: %d, height: %d", w, h); //NSLog(@"%@", imageFilename); //================================================================= //NSDictionary *metadata = [[result defaultRepresentation] metadata]; //NSLog(@"\n\nAsset Info: %@", result); //NSLog(@"\n\n\n\nMetaData: %@", metadata); [autoAssignedAssets addObject:result]; }//end if photo }//end if }; //end assetEnumerator block //[ BLOCK ] =&gt; assetGroupEnumerator // void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) { if(group != nil) { [group enumerateAssetsUsingBlock:assetEnumerator]; }//end if //now we're done, reload and stop animations [self.tableView reloadData]; [self.activity stopAnimating]; [self.activity setHidden:YES]; }; //end assetGroupEnumerator block //[ BLOCK ] =&gt; failureBlock // void (^failureBlock)(NSError *) = ^(NSError *error) { NSString *errorTitle = [error localizedDescription]; NSString *errorMessage = [error localizedRecoverySuggestion]; NSString *errorFailureDesc = [error localizedFailureReason]; NSLog(@"Error: %@, Suggestion: %@, Failure desc: %@", errorTitle, errorMessage, errorFailureDesc); }; //end failureBlock //loop over all the albums and process the pictures with the blocks above [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:assetGroupEnumerator failureBlock: failureBlock]; }//end viewDidLoad </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload