web-dev-qa-db-ja.com

iOSで画像メタデータを取得する方法

IOSを使用して画像メタデータを表示したい。絞り、シャッタースピード、露出補正、ISO、レンズの焦点距離などのメタデータ。誰かアイデアがあれば教えてください。

16
Hardik Shah
CGImageSourceRef source = CGImageSourceCreateWithURL( (CFURLRef) aUrl, NULL);
CGImageSourceRef source = CGImageSourceCreateWithData( (CFDataRef) theData, NULL);
NSDictionary* metadata = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source,0,NULL));
CFRelease(source);

辞書の内容を確認してください ここ

30
Jonas Schnelli

画像パスからメタデータを取得するためのコードは次のとおりです。

NSData *imagedata = [NSData dataWithContentsOfFile:imagePath];
CGImageSourceRef source = CGImageSourceCreateWithData((CFMutableDataRef)imagedata, NULL);
NSDictionary *metadata = [(NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source,0,NULL)autorelease];

または、Swift 4.0:

var imagedata = Data(contentsOfFile: imagePath) 
var source: CGImageSourceRef = CGImageSourceCreateWithData((imagedata as? CFMutableDataRef), nil) 
var metadata = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [AnyHashable: Any]
11
Usman Nisar

写真フレームワークでiOS11に更新

Objective --C:

#import <Photos/Photos.h>

- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

    PHAsset* asset = info[UIImagePickerControllerPHAsset];

    [asset requestContentEditingInputWithOptions:nil completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
        CIImage *fullImage = [CIImage imageWithContentsOfURL:contentEditingInput.fullSizeImageURL];

        NSLog(@"%@", fullImage.properties.description);
    }];

    [imagePicker dismissViewControllerAnimated:YES completion:nil];
}

また、フォトライブラリの使用法(NSPhotoLibraryUsageDescription)の許可が必要です。次に、次のコードを追加して、ロードされたビューまたは表示されたビューを表示できます。

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    switch (status) {
        case PHAuthorizationStatusAuthorized:
            NSLog(@"PHAuthorizationStatusAuthorized");
            break;
        case PHAuthorizationStatusDenied:
            NSLog(@"PHAuthorizationStatusDenied");
            break;
        case PHAuthorizationStatusNotDetermined:
            NSLog(@"PHAuthorizationStatusNotDetermined");
            break;
        case PHAuthorizationStatusRestricted:
            NSLog(@"PHAuthorizationStatusRestricted");
            break;
    }
}];
2
shanezzar
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];

    if(referenceURL) {
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:referenceURL resultBlock:^(ALAsset *asset) {
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        NSDictionary *metadata = rep.metadata;
        NSLog(@"image data %@", metadata);


    } failureBlock:^(NSError *error) {
        // error handling
    }];
}
2

スウィフト4

static func imageDataProperties(_ imageData: Data) -> NSDictionary? {
    if let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil)
    {
      if let dictionary = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) {
        return dictionary
      }
    }
    return nil
  }
2
kurtanamo

Swift struct:としてラップされたコアグラフィックスを介して画像プロパティを読み取る

struct ImageMetadata {

    var imageProperties : [CFString: Any]

    init?(data: Data) {
        let options = [kCGImageSourceShouldCache: kCFBooleanFalse]
        if let imageSource = CGImageSourceCreateWithData(data as CFData, options as CFDictionary),
            let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [CFString: Any] {
            self.imageProperties = imageProperties
        } else {
            return nil
        }
    }

    var dpi : Int? { imageProperties[kCGImagePropertyDPIWidth] as? Int }
    var width : Int? { imageProperties[kCGImagePropertyPixelWidth] as? Int }
    var height : Int? { imageProperties[kCGImagePropertyPixelHeight] as? Int }

}
1
Ralf Ebert

私自身の質問に答えます。 GPSメタデータを取得するためのメモリ効率が高く高速な方法は次のとおりです。

let options = [kCGImageSourceShouldCache as String: kCFBooleanFalse]
if let data = NSData(contentsOfURL: url), imgSrc = CGImageSourceCreateWithData(data, options) {
    let metadata = CGImageSourceCopyPropertiesAtIndex(imgSrc, 0, options) as Dictionary
    let gpsData = metadata[kCGImagePropertyGPSDictionary] as? [String : AnyObject]
}

2番目のオプションは

if let img = CIImage(contentsOfURL: url), metadata = img.properties(), 
gpsData = metadata[kCGImagePropertyGPSDictionary] as? [String : AnyObject] { … }

Swiftの方が見栄えがしますが、より多くのメモリを使用します(プロファイラーでテスト済み)。

0
Jaydip

画像サイズを取得するために使用しているものは次のとおりです。

+ (CGSize) sizeOfImage:(NSString *) fileName withPath: (NSURL *) fullDirectoryPath;
{
    NSURL *imageNameWithPath = [NSURL URLWithString:fileName relativeToURL:fullDirectoryPath];

    CGImageSourceRef source = CGImageSourceCreateWithURL( (CFURLRef) imageNameWithPath, NULL);
    if (!source) return CGSizeZero;

    CFDictionaryRef dictRef = CGImageSourceCopyPropertiesAtIndex(source,0,NULL);
    NSDictionary* metadata = (__bridge NSDictionary *)dictRef;
    NSLog(@"metadata= %@", metadata);
    CGSize result = CGSizeZero;
    CGFloat width = [metadata[@"PixelWidth"] floatValue];
    CGFloat height = [metadata[@"PixelHeight"] floatValue];
    NSLog(@"width= %f, height= %f", width, height);

    // The orientation in the metadata does *not* map to UIImageOrientation. Rather, see: https://developer.Apple.com/library/ios/documentation/GraphicsImaging/Reference/CGImageProperties_Reference/index.html#//Apple_ref/doc/constant_group/Individual_Image_Properties
    // This idea of orientation seems a little odd to me, but it seems it translates to even numbers need to be switched in width/height, odd numbers do not.
    NSUInteger orientation = [metadata[@"Orientation"] integerValue];

    switch (orientation) {
        // Comments give "Location of the Origin of the image"
        case 1: // Top, left
        case 3: // Bottom, right
        case 5: // Left, top
        case 7: // Right, bottom
            result = CGSizeMake(width, height);
            break;

        case 2: // Top, right
        case 4: // Bottom, left
        case 6: // Right, top
        case 8: // Left, bottom
            result = CGSizeMake(height, width);
            break;

        default:
            NSAssert(NO, @"Should not get to here!");
            break;
    }

    CFRelease(source);
    NSLog(@"size: %@, orientation: %d", NSStringFromCGSize(result), orientation);

    return result;
}

/* Example meta data:
    ColorModel = RGB;
    Depth = 8;
    Orientation = 6;
    PixelHeight = 1936;
    PixelWidth = 2592;
    "{Exif}" =     {
        ColorSpace = 1;
        PixelXDimension = 2592;
        PixelYDimension = 1936;
    };
    "{JFIF}" =     {
        DensityUnit = 0;
        JFIFVersion =         (
                               1,
                               1
                               );
        XDensity = 1;
        YDensity = 1;
    };
    "{TIFF}" =     {
        Orientation = 6;
    };
}
*/
0
Chris Prince