我想使用ios顯示圖像元數據。 元數據像孔徑,快門速度,曝光補償,ISO,鏡頭焦距等。 所以請幫助我,如果有人有想法。如何在ios中獲取圖像元數據
回答
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef) aUrl, NULL);
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef) theData, NULL);
NSDictionary* metadata = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source,0,NULL);
檢查字典內容here。
謝謝Jonas Schnelli。這對我很有幫助。謝謝哥們。 –
當然,你應該提到,你將不得不添加#import
如果你使用ARC,分割它像這樣,以避免內存泄漏 'CFDictionaryRef dictRef = CGImageSourceCopyPropertiesAtIndex(source,0,NULL); NSDictionary * metadata =(__bridge NSDictionary *)dictRef; <做你的事> CFRelease(source); CFRelease(dictRef);' –
這裏是代碼,從成像路徑獲取元數據:
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]
如何在swift3或swift4中做到這一點? –
var imagedata = Data(contentsOfFile:imagePath) var source:CGImageSourceRef = CGImageSourceCreateWithData((imagedata as?CFMutableDataRef),nil) var metadata = CGImageSourceCopyPropertiesAtIndex(source,0,nil)as? [AnyHashable:Any] –
希望它可以幫助你 –
您需要使用資產庫中讀取圖片的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) {
}];
}
希望這將有助於
不起作用,assetURL始終爲零 – malex
下面是我用得到的圖像尺寸是什麼:
+ (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;
};
}
*/
-(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
}];
}
最好在答案中給出一些解釋。 – Envil
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
}
回答我自己的問題。內存有效和快速的方式獲得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] { … }
它看起來更好斯威夫特但會佔用更多的內存(通過探查測試)。
更新到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;
}
}];
- 1. 如何從ios圖像庫的圖像中獲取數據?
- 2. 如何從ACF圖庫圖像獲取圖像元數據?
- 3. 如何從iOS中包含圖像的URL獲取數據?
- 4. 如何在iOS中獲取json數據?
- 5. 獲取DOM元素的圖像數據
- 6. 獲取APNG圖像的元數據
- 7. 從iOS中獲取圖像
- 8. 有沒有辦法在iOS中獲取圖像文件的元數據?
- 9. 如何獲取畫布圖像數據?
- 10. Javascript:如何獲取此圖像數據
- 11. 如何從圖像url中獲取圖像數據
- 12. 在.NET中獲取圖像元數據而不考慮元數據格式
- 13. 如何從IOS中的NSDictionary中獲取圖像數組
- 14. 如何在iOS中獲取圖庫圖像?
- 15. 在JavaScript中獲取圖像數據?
- 16. 在Magento 2中獲取圖像數據
- 17. 如何在圖庫視圖中從選定圖像獲取圖像數據?
- 18. 如何從圖像獲取元數據類型(英文)
- 19. 如何從數據庫中獲取數據後更改圖像
- 20. 在球拍中獲取圖像類型和元數據
- 21. 在php中獲取jpg圖像的iptc元數據
- 22. 如何獲取CGBitmap的像素數據爲Xamarin iOS中的byte []
- 23. 從MediaWiki圖像頁獲取圖像元數據
- 24. 如何從圖像中獲取像素數據使用R
- 25. 如何獲取光標下圖像框中的像素數據?
- 26. 如何在主要活動中獲取recyclerview圖像數據
- 27. 如何在數據庫中插入和獲取圖像
- 28. 如何從MySQL數據庫中獲取的圖像在PHP
- 29. vibrant.js獲取圖像數據
- 30. 如何從圖像元數據中獲取快門驅動計數android
你是什麼意思的圖像元數據(名稱,大小和其他一些)?請詳細說明... –