【问题标题】:How do I get the creation date and time of a AVURLAssett objective-c如何获取 AVURLAssett 目标 c 的创建日期和时间
【发布时间】:2021-05-16 16:14:18
【问题描述】:

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

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

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

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);
                
            }
        }

我丢弃之前在这里拥有的东西。

【问题讨论】:

    标签: ios objective-c phphotolibrary avurlasset


    【解决方案1】:

    所以我终于想通了。通过将以下代码放在后面

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

    我已经更改了我原来的帖子以反映这些更改。 现在,如果我能从资产中获取位置元数据,我就准备好了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 2010-09-19
      • 2017-11-16
      • 2011-10-16
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      相关资源
      最近更新 更多