如何在 iOS 中获取照片的原始文件名? [英] How to get a photo's original filename in iOS?

查看:67
本文介绍了如何在 iOS 中获取照片的原始文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 iPad 应用程序,用户将在文本字段中输入照片文件名(作为字段注释的一部分),然后他们会将他们的照片导入 iPad 的照片库.该应用程序将使用 ALAssetsLibrary 访问库并枚举照片,查找具有他们在字段注释中输入的文件名的照片.这将是拍摄照片的相机赋予照片的文件名.例如DSC_0019.JPG".

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".

这不可能吗?

我注意到,如果我将照片从我的相机导入 iPad,然后在我的 Mac 上打开 iPhoto 并将 iPad 视为相机,我可以获取有关 iPad 上保存的图像的信息"并查看我的原始文件名我在找然而,这并不包含在 iPad 上的元数据中.

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.

任何帮助将不胜感激.

这是我的代码:

(在使用 CFDictionary 时,除了 Exif 键没有我要找的东西外,几乎所有东西都是空的)

(In working with the CFDictionary, pretty much everything is null except the Exif keys which don't have what i'm looking for)

- (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(@"

Asset Info: %@", result);
                //NSLog(@"



MetaData: %@", 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

推荐答案

我能够像这样获得图像的原始文件名:

I was able to get an image's original filename like this:

NSString* originalFileName = [[asset defaultRepresentation] filename];

这篇关于如何在 iOS 中获取照片的原始文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆