相机胶卷图像的创建日期
Posted
技术标签:
【中文标题】相机胶卷图像的创建日期【英文标题】:Creation Date from Camera Roll Image 【发布时间】:2014-10-20 09:52:02 【问题描述】:我正在努力从相机胶卷中选择的图像中获取创建日期。 这是我的代码:
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary!)
var tmpImg = info[UIImagePickerControllerOriginalImage] as UIImage
var metaData: NSDictionary
var exifData: NSDictionary
let library = ALAssetsLibrary()
var dataJpeg: NSData
var url: NSURL = info.objectForKey(UIImagePickerControllerReferenceURL) as NSURL
library.assetForURL(url, resultBlock:
(asset: ALAsset!) in
if asset != nil
var image_representation : ALAssetRepresentation = asset.defaultRepresentation()
var iref : CGImage = assetRep.fullResolutionImage().takeUnretainedValue()
//How do I get DateTime from CGImage in Swift??
, failureBlock:
(error: NSError!) in
NSLog("Error!")
)
在带有 Objective-C 的旧 Xcode 版本中,我这样做了,效果很好:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
//////////////////////////
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL]
resultBlock:^(ALAsset *asset)
ALAssetRepresentation *image_representation = [asset defaultRepresentation];
// create a buffer to hold image data
uint8_t *buffer = (Byte*)malloc(image_representation.size);
NSUInteger length = [image_representation getBytes:buffer fromOffset: 0.0 length:image_representation.size error:nil];
if (length != 0)
// buffer -> NSData object; free buffer afterwards
NSData *adata = [[NSData alloc] initWithBytesNoCopy:buffer length:image_representation.size freeWhenDone:YES];
// identify image type (jpeg, png, RAW file, ...) using UTI hint
NSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys:(id)[image_representation UTI] ,kCGImageSourceTypeIdentifierHint,nil];
// create CGImageSource with NSData
CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef) adata, (__bridge CFDictionaryRef) sourceOptionsDict);
// get imagePropertiesDictionary
CFDictionaryRef imagePropertiesDictionary;
imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(sourceRef,0, NULL);
// get exif data
CFDictionaryRef exif = (CFDictionaryRef)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyExifDictionary);
NSDictionary *exif_dict = (__bridge NSDictionary*)exif;
NSString *creationdate=exif_dict[@"DateTimeOriginal"];
// clean up
CFRelease(imagePropertiesDictionary);
CFRelease(sourceRef);
else
NSLog(@"image_representation buffer length == 0");
failureBlock:^(NSError *error)
NSLog(@"couldn't get asset: %@", error);
];
如果有人能帮我从图像中取出 CreationDate,我将不胜感激。
【问题讨论】:
您找到问题的解决方案了吗?如果没有,请查看***.com/a/36531297/1035008。 【参考方案1】:基于您的代码和 How can I get image created date time or last modified date time from ios Photos (like camera roll)?
这个适用于我的 XCode 6.1.1 for Swift:
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary!)
let library = ALAssetsLibrary()
var url: NSURL = info.objectForKey(UIImagePickerControllerReferenceURL) as NSURL
library.assetForURL(url, resultBlock:
(asset: ALAsset!) in
if asset != nil
//print the creation date
println(asset.valueForProperty(ALAssetPropertyDate))
, failureBlock:
(error: NSError!) in
NSLog("Error!")
)
【讨论】:
【参考方案2】:自己找到了解决方案:
var image_representation : ALAssetRepresentation = asset.defaultRepresentation()
var iref : CGImage = image_representation.fullResolutionImage().takeUnretainedValue()
var metaData : NSDictionary = image_representation.metadata()
【讨论】:
【参考方案3】:这里是用于检查创建日期的 swift 3 代码
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
let library = ALAssetsLibrary()
let url: NSURL = info[UIImagePickerControllerReferenceURL] as! NSURL
library.asset(for: url as URL, resultBlock: (asset: ALAsset?) in
if asset != nil
//print the creation date
print(asset!.value(forProperty: ALAssetPropertyDate))
, failureBlock: (error: Error?) in
NSLog("Error \(error)")
)
【讨论】:
以上是关于相机胶卷图像的创建日期的主要内容,如果未能解决你的问题,请参考以下文章
将 PageViewController 用于大量页面,如相机胶卷图像视图?