从照片库中获取 ALAssets iOS

getting ALAssets from photos library iOS

我正在尝试从我拍摄的电影中获取时间、日期和位置 iPhone/iPad 这是我使用的代码

//我选择了视频,然后将其保存到应用程序,但我想我在保存之前获取了电影的资产数据。

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{   NSError *error;
 else if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {

    NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:movieURL
                   resultBlock:^(ALAsset*asset) {
                       NSDate *myDate = [asset valueForProperty:ALAssetPropertyDate];
                       NSLog(@"Date: %@", myDate);

                   } failureBlock:^(NSError *error) {
                       NSLog(@"Error");
                   }];
    NSData * movieData = [NSData dataWithContentsOfURL:movieURL];
    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];
} 

但是 myDate 被记录为 null

我做错了什么

我也刚试过这个代码(添加 10/11)

CGImageSourceRef source = CGImageSourceCreateWithURL( (CFURLRef)movieURL, NULL);

    NSDictionary* metadata = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source,0,NULL));
    NSLog(@"image data %@", metadata);

但元数据仍然为空;

我了解到视频没有与图像相同的元数据。如果这是真的,那么我该如何访问视频元数据,因为我可以在照片中看到与视频相关的数据。

如有任何帮助,我们将不胜感激。

我已经尝试了我在互联网上可以找到的所有方法来帮助我做到这一点,但似乎没有任何效果。 当我登录我的 movieURL 时,这就是我得到的

2016-10-13 09:58:51.045271 my little world app[2641:1388034]     movieURL;file:///private/var/mobile/Containers/Data/Application/22A178E5-74C4-    48EF-B487-5E01321508AD/tmp/trim.04F0AC8A-2960-464D-8670-7E79662EAB9B.MOV

因为它在临时文件中,所以我无法获取元数据。

元数据是否存在于电影 NSData 中,我可以从中获取吗?

我正在拔头发

所以我想出了这就是代码

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {
 NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
 [self getMediaName:nil url:[info objectForKey:UIImagePickerControllerReferenceURL]];
}
}
- (void)getMediaName:(UIImage*)originalImage url:(NSURL*)url {
    @try {
        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) {
            if (asset == nil) return;
            ALAssetRepresentation *assetRep = [asset defaultRepresentation];
            NSString *fileName = [assetRep filename];
 NSDate *myDate = [asset valueForProperty:ALAssetPropertyDate];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"MM-dd-yyyy"];
            NSLog(@"%@",[dateFormatter stringFromDate:myDate]);
ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *error) {

        };

        ALAssetsLibrary *library = [ALAssetsLibrary new];
        [library assetForURL:url resultBlock:resultblock failureBlock:failureblock];
    }
    @catch (NSException *exception) {

    }
}

您可以使用任何 valueForProperty