我有一个应用程序,我从一个照片库中挑选一个视频,我想从它的元数据中获得它被创建的日期和时间(如果可能的话,它被拍摄的位置),这样我就可以用它来做一些标签。我可以对从图片库中挑选的图像这样做,但它不适用于视频。这是我的图片,但我似乎不能适应一个视频。
所以我做了一些调整,像这样:
//我从照片库中获取视频
if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {
NSLog(@"info:%@",info);
NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
NSData * movieData = [NSData dataWithContentsOfURL:movieURL];
//我将它保存到documentsDirectory中,并在中间点获得图像
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 = [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];
我已经修改了我原来的帖子来反映这些变化。现在,如果我能从资产中获得位置的元数据,我就万事通了。