iOS UIImagePickerController:获取所选图片的date的任何方式?
在我的应用程序中,我正在使用调用的UIImagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
成功。 事情是,我需要拍摄图像的date。 如果用户拍摄了新的照片,我可以select当前的date,但是如果用户从相机胶卷或其他保存的照片中select了一张照片,我该怎么办?
您可以使用此代码从相册中提取照片和video。 info是所提到的委托方法中的第二个参数。
NSURL *mediaUrl = info[UIImagePickerControllerReferenceURL]; ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; [assetsLibrary assetForURL:mediaUrl resultBlock:^(ALAsset *asset) { NSDate *date = [asset valueForProperty:ALAssetPropertyDate]; // ... } failureBlock:nil];
同样,为了使其工作,您需要包含项目AssetsLibrary框架。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSDictionary *metadataDictionary = (NSDictionary *)[info valueForKey:UIImagePickerControllerMediaMetadata]; // do something with the metadata NSLog(@"meta : %@ \n\n",metadataDictionary); }
然后
你必须得到“DateTime”键的值
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { if let assertURL = info[UIImagePickerControllerReferenceURL] as? NSURL { let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([assertURL], options: nil) if let asset = fetchResult.firstObject as? PHAsset { // Here is the date when your image was taken print(asset.creationDate) } } }
确保也包含import Photos
的Photos.framework。