2011-08-24 10 views
14

私は現在、(フィールドノートの一部として)ユーザーがテキストフィールドに写真のファイル名を入力し、その後写真をiPadのフォトライブラリにインポートするiPadアプリを開発しています。アプリケーションは、ALAssetsLibraryを使用してライブラリにアクセスし、写真の上に列挙し、フィールドノートに入力したファイル名のものを探します。これはカメラで撮影したファイル名です。たとえば、 "DSC_0019.JPG"とします。iOSで写真の元のファイル名を取得する方法は?

これはできませんか?

カメラからiPadに写真をインポートしてからMacでiPhotoを開き、iPadをカメラとして見ると、iPadに保存されている画像に「情報を取得」して元のファイル名を探しています。ただし、これはiPadのメタデータには含まれていません。

ご協力いただければ幸いです。ここで

私のコードです:

(CFDictionaryでの作業では、ほとんどすべてが私が探しているものはありませんExifのキー以外nullである)

- (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 
+0

を、私はこの答えを把握しようとしているときを過ごした(本当に...楽しみのために!)。唯一目指していたのがこの質問でした - http://stackoverflow.com/questions/5048640/retrieving-a-filename-for-an-alasset - またこの興味深いメタデータチュートリアル - http://sarofax.wordpress .com/2011/07/10/alasset-image-metadata /。私はさらに助けてくれることを望みます。 Appleのビルトインアプリは、明らかに秘密のリスをやっている。 –

答えて

21

私はこのような画像の元のファイル名を取得することができました:

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

実際に私は今これが元のファイル名を与えていないことを認識しています。私はその時に使っていたファイルに似ているIMG_0101.JPGのようなものを返します。 – djschwartz

+2

私はあなたが私の心配していたあなたのデバイスに写真をインポートする場合、これは実際には元のファイル名であることがわかりました。だから私のニコンからは、この方法を使ったDSC_0001.jpgがありました。 – bwizzy

+3

これはiOS 5.xでのみ動作します – Seunghoon

0

抽出

0:ALAssetからURLは、次のようになります。資産ライブラリ:?あなたは別のファイル名が必要な場合は、内部外部のパラダイムを作る

=十億一& EXT //asset/asset.JPG ID = 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メソッドを使用します。

関連する問題