简体   繁体   English

从照片库iOS获取ALAsets

[英]getting ALAssets from photos library iOS

I am trying to get the time, date and location from a movie i took on my iPhone/iPad This is the code i used 我试图从我在iPhone / iPad上拍摄的电影中获取时间,日期和位置,这是我使用的代码

//i get the video picked and then save it to app but i think i am getting the movie's asset data before i save it. //我选择了视频,然后将其保存到应用程序中,但是我认为在保存之前,我正在获取电影的资产数据。

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

however myDate is logged as null 但是myDate记录为null

what am i doing wrong 我究竟做错了什么

I just tried this code also (added 10/11) 我也尝试过此代码(添加10/11)

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

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

but still metadata is null; 但元数据仍然为null;

I read that videos don't have the same metadata as images. 我读到视频没有与图像相同的元数据。 if this is true then how do I access the video metadata since I can see data tied to the video in photos. 如果是这样,那么如何访问视频元数据,因为我可以在照片中看到与视频相关的数据。

any help would be greatly appreciated. 任何帮助将不胜感激。

I have tried everything I could find on the internet to help me do this but nothing seems to work. 我已经尽力在互联网上可以找到的所有方法来帮助我完成此任务,但似乎没有任何效果。 When I log my movieURL this is what I get 当我登录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

So since it is in a temporary file can I not get the metadata. 因此,由于它在一个临时文件中,因此无法获取元数据。

Does the Metadata exits in the movies NSData and can I get it from that? 电影中的元数据是否存在元数据,我可以从中获取元数据吗?

I am pulling my hair out. 我正在拔头发。

So I figured this out here is the code 所以我发现这里是代码

- (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) {

    }
}

you can use any valueForProperty 您可以使用任何valueForProperty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM