如何获取 AVURLAssett objective-c 的创建日期和时间
How do I get the creation date and time of a AVURLAssett objective-c
我有一个应用程序,我从照片库中选择一个视频,我想从它的元数据中获取它的创建日期和时间(如果可能的话还有它的拍摄位置),以便我可以将它用于一些标签。我可以为从照片库中挑选的图像执行此操作,但它不适用于视频。这是我的图片,但我似乎无法将其改编成视频。
所以我做了一些这样的改编:
//我从图片库中获取视频
if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {
NSLog(@"info:%@",info);
NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
NSData * movieData = [NSData dataWithContentsOfURL:movieURL];
//我保存到documents目录,在中途获取一张图片
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *slash = [documentsDirectory stringByAppendingPathComponent:@"/"];
NSString *documentsDirectory2 = [slash stringByAppendingPathComponent:self.user.text];
NSString *documentsDirectory21 = [documentsDirectory2 stringByAppendingPathComponent:@"/Movie"];
if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory21])
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory21 withIntermediateDirectories:NO attributes:nil error:&error];
NSString *fullPath = [documentsDirectory21 stringByAppendingPathComponent:[[self imageNameTextField]text]];
fullPath = [fullPath stringByAppendingFormat:@".mp4"];
[ movieData writeToFile:fullPath atomically:YES];
AVURLAsset *anAsset = [[AVURLAsset alloc] initWithURL:movieURL options:nil];
//这是我改的
PHAsset *theAsset = [info valueForKey:UIImagePickerControllerMediaURL] ;
NSLog(@"creationDate1:%@",theAsset);
NSLog(@"creationDate2:%@",anAsset.creationDate.value);
NSDate *creationDate =(NSDate *)anAsset.creationDate.value;
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM-dd-yyyy"];
dayString = [dateFormatter stringFromDate:creationDate];
NSDateFormatter *dateFormatter2=[[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"hh:mm a"];
timeString = [dateFormatter2 stringFromDate:creationDate];
if ([[anAsset tracksWithMediaType:AVMediaTypeVideo] count] > 0) {
AVAssetImageGenerator *imageGenerator =
[AVAssetImageGenerator assetImageGeneratorWithAsset:anAsset];
Float64 durationSeconds = CMTimeGetSeconds([anAsset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
NSError *error;
CMTime actualTime;
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];
if (halfWayImage != NULL) {
NSString *actualTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
NSString *requestedTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, midpoint));
NSLog(@"Got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString);
myImage.image = [UIImage imageWithCGImage:halfWayImage scale:1.0 orientation:UIImageOrientationRight];
// Do something interesting with the image.
CGImageRelease(halfWayImage);
}
}
丢弃之前我在这里拥有的东西。
所以我终于弄明白了。通过在
之后放置以下代码
AVURLAsset *anAsset = [[AVURLAsset alloc] initWithURL:movieURL options:nil];
PHAsset *theAsset = [信息 valueForKey:UIImagePickerControllerMediaURL] ;
NSLog(@"creationDate1:%@",theAsset);
NSLog(@"creationDate2:%@",anAsset.creationDate.value);
NSDate *creationDate =(NSDate *)anAsset.creationDate.value;
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM-dd-yyyy"];
dayString = [dateFormatter stringFromDate:creationDate];
NSDateFormatter *dateFormatter2=[[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"hh:mm a"];
timeString = [dateFormatter2 stringFromDate:creationDate];
我已经更改了原来的 post 以反映这些更改。
现在,如果我可以从资产中获取位置元数据,我就万事大吉了。
我有一个应用程序,我从照片库中选择一个视频,我想从它的元数据中获取它的创建日期和时间(如果可能的话还有它的拍摄位置),以便我可以将它用于一些标签。我可以为从照片库中挑选的图像执行此操作,但它不适用于视频。这是我的图片,但我似乎无法将其改编成视频。
所以我做了一些这样的改编:
//我从图片库中获取视频
if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {
NSLog(@"info:%@",info);
NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
NSData * movieData = [NSData dataWithContentsOfURL:movieURL];
//我保存到documents目录,在中途获取一张图片
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *slash = [documentsDirectory stringByAppendingPathComponent:@"/"];
NSString *documentsDirectory2 = [slash stringByAppendingPathComponent:self.user.text];
NSString *documentsDirectory21 = [documentsDirectory2 stringByAppendingPathComponent:@"/Movie"];
if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory21])
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory21 withIntermediateDirectories:NO attributes:nil error:&error];
NSString *fullPath = [documentsDirectory21 stringByAppendingPathComponent:[[self imageNameTextField]text]];
fullPath = [fullPath stringByAppendingFormat:@".mp4"];
[ movieData writeToFile:fullPath atomically:YES];
AVURLAsset *anAsset = [[AVURLAsset alloc] initWithURL:movieURL options:nil];
//这是我改的
PHAsset *theAsset = [info valueForKey:UIImagePickerControllerMediaURL] ;
NSLog(@"creationDate1:%@",theAsset);
NSLog(@"creationDate2:%@",anAsset.creationDate.value);
NSDate *creationDate =(NSDate *)anAsset.creationDate.value;
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM-dd-yyyy"];
dayString = [dateFormatter stringFromDate:creationDate];
NSDateFormatter *dateFormatter2=[[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"hh:mm a"];
timeString = [dateFormatter2 stringFromDate:creationDate];
if ([[anAsset tracksWithMediaType:AVMediaTypeVideo] count] > 0) {
AVAssetImageGenerator *imageGenerator =
[AVAssetImageGenerator assetImageGeneratorWithAsset:anAsset];
Float64 durationSeconds = CMTimeGetSeconds([anAsset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
NSError *error;
CMTime actualTime;
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];
if (halfWayImage != NULL) {
NSString *actualTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
NSString *requestedTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, midpoint));
NSLog(@"Got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString);
myImage.image = [UIImage imageWithCGImage:halfWayImage scale:1.0 orientation:UIImageOrientationRight];
// Do something interesting with the image.
CGImageRelease(halfWayImage);
}
}
丢弃之前我在这里拥有的东西。
所以我终于弄明白了。通过在
之后放置以下代码AVURLAsset *anAsset = [[AVURLAsset alloc] initWithURL:movieURL options:nil];
PHAsset *theAsset = [信息 valueForKey:UIImagePickerControllerMediaURL] ;
NSLog(@"creationDate1:%@",theAsset);
NSLog(@"creationDate2:%@",anAsset.creationDate.value);
NSDate *creationDate =(NSDate *)anAsset.creationDate.value;
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM-dd-yyyy"];
dayString = [dateFormatter stringFromDate:creationDate];
NSDateFormatter *dateFormatter2=[[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"hh:mm a"];
timeString = [dateFormatter2 stringFromDate:creationDate];
我已经更改了原来的 post 以反映这些更改。 现在,如果我可以从资产中获取位置元数据,我就万事大吉了。