2012-09-18 49 views
11

我想使用ios顯示圖像元數據。 元數據像孔徑,快門速度,曝光補償,ISO,鏡頭焦距等。 所以請幫助我,如果有人有想法。如何在ios中獲取圖像元數據

+0

你是什麼意思的圖像元數據(名稱,大小和其他一些)?請詳細說明... –

回答

26
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef) aUrl, NULL); 
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef) theData, NULL); 
NSDictionary* metadata = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source,0,NULL); 

檢查字典內容here

+0

謝謝Jonas Schnelli。這對我很有幫助。謝謝哥們。 –

+4

當然,你應該提到,你將不得不添加#import 到你的標題,以便使用該類... –

+1

如果你使用ARC,分割它像這樣,以避免內存泄漏 'CFDictionaryRef dictRef = CGImageSourceCopyPropertiesAtIndex(source,0,NULL); NSDictionary * metadata =(__bridge NSDictionary *)dictRef; <做你的事> CFRelease(source); CFRelease(dictRef);' –

4

這裏是代碼,從成像路徑獲取元數據:

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

如何在swift3或swift4中做到這一點? –

+1

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

+0

希望它可以幫助你 –

-1

您需要使用資產庫中讀取圖片的EXIF元。

#import <AssetsLibrary/AssetsLibrary.h> 

,然後添加以下代碼:

- (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) { 
      }]; 
} 

希望這將有助於

+0

不起作用,assetURL始終爲零 – malex

0

下面是我用得到的圖像尺寸是什麼:

+ (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; 
    }; 
} 
*/ 
1
-(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

最好在答案中給出一些解釋。 – Envil

0

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

回答我自己的問題。內存有效和快速的方式獲得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] 
} 

第二個選項是

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

它看起來更好斯威夫特但會佔用更多的內存(通過探查測試)。

1

更新到iOS 11張照片框架

的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; 
    } 
}]; 
相關問題