How to get image metadata in ios

后端 未结 9 1552
独厮守ぢ
独厮守ぢ 2020-11-29 06:18

I want to display image metadata using ios. Meta data like Aperture, Shutterspeed, Exposure Compensation, ISO, Lens Focal Length, etc. So please help me if anybody has idea

相关标签:
9条回答
  • 2020-11-29 06:34

    swift 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
      }
    
    0 讨论(0)
  • 2020-11-29 06:35

    Here is code, to get meta data from an image path:

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

    Or, if using 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]
    
    0 讨论(0)
  • 2020-11-29 06:35

    You need to use assets library to read exif metadata of image.

    #import <AssetsLibrary/AssetsLibrary.h>
    

    And then add following code :

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        __block NSMutableDictionary *imageMetadata = nil;
        [library assetForURL:assetURL
                 resultBlock:^(ALAsset *asset)  {
                     NSDictionary *metadata = asset.defaultRepresentation.metadata;
                     imageMetadata = [[NSMutableDictionary alloc] initWithDictionary:metadata];
                     NSLog(@"%@",imageMetadata.description);
                 }
                failureBlock:^(NSError *error) {
                }];
    }
    

    Hope this will help

    0 讨论(0)
  • 2020-11-29 06:43
    -(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
        }];
    }
    
    0 讨论(0)
  • 2020-11-29 06:44
    CGImageSourceRef source = CGImageSourceCreateWithURL( (CFURLRef) aUrl, NULL);
    CGImageSourceRef source = CGImageSourceCreateWithData( (CFDataRef) theData, NULL);
    NSDictionary* metadata = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source,0,NULL));
    CFRelease(source);
    

    Check the dictionary contents here.

    0 讨论(0)
  • 2020-11-29 06:47

    Reading image properties via Core Graphics wrapped as 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 }
    
    }
    
    0 讨论(0)
提交回复
热议问题