我目前正在开发一个iPad应用程序,用户将在文本字段中输入照片文件名(作为现场笔记的一部分),然后他们将他们的照片导入iPad的照片库。该应用程序将使用ALAssetsLibrary访问该库并枚举照片,查找具有它们在现场笔记中输入的文件名的文件。这将是相机所拍摄的照片的文件名。例如“DSC_0019.JPG”。如何在iOS中获取照片的原始文件名?
这不可能吗?
我注意到如果我将照片从我的相机导入iPad,然后在我的Mac上打开iPhoto,并将iPad作为相机查看,我可以“获取iPad上的图像”并查看原始文件名我在找。但是,这不包含在iPad的元数据中。
任何帮助将不胜感激。
这里是我的代码:
(与CFDictionary工作,几乎一切都是除的Exif键空不有什么我要找的)
- (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 ] => 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, &w);
CFNumberGetValue(imageHeight, kCFNumberIntType, &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 ] => 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 ] => 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
我花了一段时间试图找出这个答案。我做的唯一的进展是这个问题 - http://stackoverflow.com/questions/5048640/retrieving-a-filename-for-an-alasset - 也是这个有趣的元数据教程 - http://sarofax.wordpress .com/2011/07/10/alasset-image-metadata /。我希望我能进一步帮助。苹果的内置应用显然是在那里做一些秘密松鼠的东西。 –