15

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

这不可能吗?

我注意到,如果我将照片从相机导入 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
4

2 回答 2

21

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

NSString* originalFileName = [[asset defaultRepresentation] filename];
于 2011-11-19T20:06:33.667 回答
0

从 ALAsset 中提取 URL,如下所示: assets-library://asset/asset.JPG?id=1000000001&ext=JPG

如果您需要不同的文件名,请创建一个内部-外部范例:

#import <Foundation/Foundation.h>

@interface NSURL (NSURL_Asset)
- (NSURL*) toExternalForm;
- (NSURL*) fromExternalForm;
- (NSString*) toExternalFilename;    

@end

#import "NSURL+Asset.h"
#import "URLParser.h" // from http://iphone.demay-fr.net/2010/04/parsing-url-parameters-in-a-nsstring/

static NSString *const EXTERNAL_TOKEN = @"/assetExternalForm/";
@implementation NSURL (NSURL_Asset)

// assets-library://asset/asset.JPG/assetExternalForm/1000000001.JPG -> assets-library://asset/asset.JPG?id=1000000001&ext=JPG
- (NSURL*) fromExternalForm {
    if([self.scheme isEqualToString:@"assets-library"]) {
        NSRange slash = [self.absoluteString rangeOfString:EXTERNAL_TOKEN options:NSBackwardsSearch];
        if(slash.location != NSNotFound) {

            NSRange dot = [self.absoluteString rangeOfString:@"." options:NSBackwardsSearch];

            if(dot.location != NSNotFound) {
                NSString* extention = [self.absoluteString substringFromIndex:(dot.location + dot.length)];
                NSString* identifier = [self.absoluteString substringWithRange:NSMakeRange(slash.location + slash.length, dot.location - (slash.location + slash.length))];
                return [NSURL URLWithString:[NSString stringWithFormat:@"%@?id=%@&ext=%@", [self.absoluteString substringToIndex:slash.location], identifier, extention]];
            }
        }
    }
    return self;
}
// assets-library://asset/asset.JPG?id=1000000001&ext=JPG -> assets-library://asset/asset.JPG/assetExternalForm/1000000001.JPG
- (NSURL*) toExternalForm {
    if([self.scheme isEqualToString:@"assets-library"]) {
        NSRange range = [self.absoluteString rangeOfString:@"?"];
        if(range.location != NSNotFound) {
            URLParser *parser = [[[URLParser alloc] initWithURLString:self.absoluteString] autorelease];
            NSString* extention = [parser valueForVariable:@"ext"];
            NSString* identifier = [parser valueForVariable:@"id"];
            if(extention != NULL && identifier != NULL) {
                return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@.%@", [self.absoluteString substringToIndex:range.location], EXTERNAL_TOKEN, identifier, extention]];
            }
        }
    }
    return self;
}
// assets-library://asset/asset.JPG?id=1000000001&ext=JPG -> 1000000001.JPG
- (NSString*) toExternalFilename {
    if([self.scheme isEqualToString:@"assets-library"]) {
        NSRange range = [self.absoluteString rangeOfString:@"?"];
        if(range.location != NSNotFound) {
            URLParser *parser = [[[URLParser alloc] initWithURLString:self.absoluteString] autorelease];
            NSString* extention = [parser valueForVariable:@"ext"];
            NSString* identifier = [parser valueForVariable:@"id"];

            if(extention != NULL && identifier != NULL) {
                return [NSString stringWithFormat:@"%@.%@", identifier, extention];
            }
        }
    }
    return NULL;
}
@end

请注意,您不需要文件名来读取 ALAsset 的内容。为此使用 ALAsset.defaultRepresentation.getBytes 方法。

于 2011-11-01T14:11:35.413 回答