0

I'm trying to retrieve relevant metadata for a photo using ALAssetsLibrary. The below code works perfectly for all data except file size - kCGImagePropertyFileSize. Also, how can I retrieve the image format?

Thanks.

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        [group setAssetsFilter:[ALAssetsFilter allAssets]];
        [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:photoNumber] options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
            if (alAsset) {
                if ([[alAsset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]){
                    ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                    NSDictionary *imageMetadata = [representation metadata];

                    NSDictionary *tiffDictionary = [imageMetadata objectForKey:(NSString *)kCGImagePropertyTIFFDictionary];
                    NSString *cameraMake = [tiffDictionary objectForKey:(NSString *)kCGImagePropertyTIFFMake];
                    NSString *cameraModel = [tiffDictionary objectForKey:(NSString *)kCGImagePropertyTIFFModel];
                    NSString *cameraSoftware = [tiffDictionary objectForKey:(NSString *)kCGImagePropertyTIFFSoftware];
                    NSString *photoCopyright = [tiffDictionary objectForKey:(NSString *)kCGImagePropertyTIFFCopyright];


                    NSDictionary *exifDictionary = [imageMetadata objectForKey:(NSString *)kCGImagePropertyExifDictionary];
                    NSString *lensMake = [exifDictionary objectForKey:(NSString *)kCGImagePropertyExifLensMake];
                    NSString *lensModel = [exifDictionary objectForKey:(NSString *)kCGImagePropertyExifLensModel];
                    NSString *exposureTime = [exifDictionary objectForKey:(NSString *)kCGImagePropertyExifExposureTime];
                    NSString *exposureProgram = [exifDictionary objectForKey:(NSString *)kCGImagePropertyExifExposureProgram];
                    NSString *ISOSpeed = [exifDictionary objectForKey:(NSString *)kCGImagePropertyExifISOSpeedRatings];
                    NSString *dateTime = [exifDictionary objectForKey:(NSString *)kCGImagePropertyExifDateTimeOriginal];
                    NSString *shutterSpeed = [exifDictionary objectForKey:(NSString *)kCGImagePropertyExifShutterSpeedValue];
                    NSString *apertureValue = [exifDictionary objectForKey:(NSString *)kCGImagePropertyExifApertureValue];
                    NSString *focalLength = [exifDictionary objectForKey:(NSString *)kCGImagePropertyExifFocalLength];
                    NSString *whiteBalance = [exifDictionary objectForKey:(NSString *)kCGImagePropertyExifWhiteBalance];


                    NSString *photoSize = [imageMetadata objectForKey:(NSString *)kCGImagePropertyFileSize];
                    NSString *photoWidth = [imageMetadata objectForKey:(NSString *)kCGImagePropertyPixelWidth];
                    NSString *photoHeight = [imageMetadata objectForKey:(NSString *)kCGImagePropertyPixelHeight];

                    NSDictionary *gpsDictionary = [imageMetadata objectForKey:(NSString *)kCGImagePropertyGPSDictionary];
                    NSString *gpsLatitude = [gpsDictionary objectForKey:(NSString *)kCGImagePropertyGPSLatitude];
                    NSString *gpsLatitudeRef = [gpsDictionary objectForKey:(NSString *)kCGImagePropertyGPSLatitudeRef];
                    NSString *gpsLongitude = [gpsDictionary objectForKey:(NSString *)kCGImagePropertyGPSLongitude];
                    NSString *gpsLongitudeRef = [gpsDictionary objectForKey:(NSString *)kCGImagePropertyGPSLongitudeRef];

                    NSLog(@"MAKE = %@", cameraMake);
                    NSLog(@"MODEL = %@", cameraModel);
                    NSLog(@"SOFTWARE = %@", cameraSoftware);
                    NSLog(@"COPYRIGHT = %@", photoCopyright);
                    NSLog(@"LENS MAKE = %@", lensMake);
                    NSLog(@"LENS MODEL = %@", lensModel);
                    NSLog(@"EXPOSURE TIME = %@", exposureTime);
                    NSLog(@"EXPOSURE PROGRAM = %@", exposureProgram);
                    NSLog(@"ISO SPEED = %@", ISOSpeed);
                    NSLog(@"DATE & TIME = %@", dateTime);
                    NSLog(@"SHUTTER SPEED = %@", shutterSpeed);
                    NSLog(@"APERTURE VALUE = %@", apertureValue);
                    NSLog(@"FOCAL LENGTH = %@", focalLength);
                    NSLog(@"WHITE BALANCE = %@", whiteBalance);
                    NSLog(@"IMAGE SIZE = %@", photoSize);
                    NSLog(@"IMAGE WIDTH = %@", photoWidth);
                    NSLog(@"IMAGE HEIGHT = %@", photoHeight);
                    NSLog(@"GPS Coordinates: %@ %@ / %@ %@", gpsLatitude, gpsLatitudeRef, gpsLongitude, gpsLongitudeRef);
                }
                else if([[alAsset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]){

                }
            }
        }];
    }
                         failureBlock: ^(NSError *error2) {
                         }];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user1752054
  • 372
  • 4
  • 17

1 Answers1

2
long long dataSize = [asset.defaultRepresentation size];
B.S.
  • 21,660
  • 14
  • 87
  • 109
  • Thanks George for editing my question and for your answer. The file size works perfectly. What about the file format (i.e. JPEG, PNG, TIFF, etc.)? – user1752054 May 28 '14 at 15:11
  • This method returs the size of the file, it doesn't matter what extension do you have. – B.S. May 28 '14 at 15:22
  • thanks. I need to display the file extension in a metadata info tab. That's why I need the file format. – user1752054 May 28 '14 at 15:31
  • From [asset.defaultRepresentation.url absoluteString] you can retriev file extension – B.S. May 28 '14 at 15:35
  • I can see the file extension in the URL but how can I place just that in an NSString? – user1752054 May 28 '14 at 16:16
  • http://stackoverflow.com/questions/9245123/get-the-extension-of-a-file-contained-in-an-nsstring – B.S. May 28 '14 at 16:19