如何获取 AVURLAssett Objective-c 的创建日期和时间

问题描述

我有一个应用程序,我从照片库中挑选视频,我想从它的元数据中获取它的创建日期和时间(如果可能,还有它的拍摄位置),以便我可以将它用于一些标签.我可以对从照片库中选取的图像执行此操作,但它不适用于视频。这是我的图像,但我似乎无法将其调整为视频。

所以我做了一些这样的改编:

//我从照片库中获取视频

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];

我已更改我的原始帖子以反映更改。 现在,如果我能从资产中获取位置的元数据,我就大功告成了。