将元数据保存到savedPhotoAlbum Xcode中的视频

时间:2016-10-28 01:56:16

标签: ios xcode metadata uiimagepickercontroller alassetslibrary

当我使用UIImagePickerController拍摄视频并使用

将其保存到SavedPhotosAlbum时
 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
   [library writeVideoAtPathToSavedPhotosAlbum:(NSURL *)movieURL
    completionBlock:^(NSURL *assetURL, NSError *error){
    if (error) {
    NSLog(@"Save video fail:%@",error);
    } else {
    NSLog(@"Save video succeed.");
     [self getsavedvideo];
     }}];

保存的文件包含名称和创建日期,但不包含GPS信息。我很惊讶,因为我使用的是内置摄像头和imagePicker。为什么Apple不会只包含GPS坐标(即使我不得不要求用户使用位置服务)。

无论如何我离题

我还没有找到将GPS坐标添加到视频中,因为它正在保存,我无法找到在completionBlock中添加此信息。

如果有人知道请告诉我。

所以我写了一些在保存视频后调用的代码[self getsavedvideo];在上面获取已保存的视频,我可以获取电影名称和创建日期,它显示没有位置信息。

 -(void) getsavedvideo;{
   ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
  // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
   [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group,     BOOL *stop) {
   // Within the group enumeration block, filter to enumerate just photos.
  [group setAssetsFilter:[ALAssetsFilter allVideos]];
    // Chooses the photo at the last index
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset,   NSUInteger index, BOOL *innerStop) {
     // The end of the enumeration is signaled by asset == nil.
      if (alAsset) {
     ALAssetRepresentation *representation = [alAsset defaultRepresentation];
     // Stop the enumerations
     *stop = YES; *innerStop = YES;
      // Do something interesting with the AV asset.
      NSString *fileName = [representation filename];
       NSDate *myDate = [alAsset valueForProperty:ALAssetPropertyDate];
       CLLocation *location = [alAsset valueForProperty:ALAssetPropertyLocation];
       NSLog(@"fileName!!!!,%@",fileName);
       NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setDateFormat:mad:"dd-MM-yyyy 'at' HH:mm"];
       NSLog( @"date for this file is %@ %@", [[[alAsset defaultRepresentation] url] absoluteString],   [dateFormatter stringFromDate: myDate] );
        NSLog (@"locate!!!!%@",location);
         } }];
        } failureBlock: ^(NSError *error) {
         NSLog(@"No groups");}];}

所以有人知道如何使用GPS信息附加此信息并将其保存回视频的元数据。 我已经在脑子里绞了几天,并没有真正发现任何有用的东西。

Plz帮助

1 个答案:

答案 0 :(得分:0)

所以我想出了一个运行ios8或更新版本的设备。

将此代码添加到上面的completionBlock中的 - (void)getsavedvideo。

确保你有

import

if ([PHAsset class]) { // If this class is available, we're running iOS 8

        PHFetchOptions *fetchOptions = [PHFetchOptions new];
        fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
        fetchOptions.fetchLimit = 1;
        PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:fetchOptions];
        PHAsset *lastImageAsset = [fetchResult lastObject];

        [[PHImageManager defaultManager]requestImageForAsset:lastImageAsset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:nil resultHandler:^(UIImage *result, NSDictionary *info){
            if ([info objectForKey:PHImageErrorKey] == nil && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue]) {

                NSArray *resources = [PHAssetResource assetResourcesForAsset:lastImageAsset];
                NSString *orgFilename = ((PHAssetResource*)resources[0]).originalFilename;



                [lastImageAsset requestContentEditingInputWithOptions:nil
                                           completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
                                               NSURL *imageURL = contentEditingInput.fullSizeImageURL;
                                               NSString *urlstring = [imageURL absoluteString];
                                               NSLog(@"urlstring%@",urlstring);
                                           }];



                CLLocationCoordinate2D    locationNew = CLLocationCoordinate2DMake( currentLocation.coordinate.latitude, currentLocation.coordinate.longitude) ;
                NSDate *nowDate = [NSDate date];

                CLLocation *myLocation = [[CLLocation alloc ]initWithCoordinate:locationNew altitude:0.0 horizontalAccuracy:1.0 verticalAccuracy:1.0 timestamp:nowDate];


                [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                    // Create a change request from the asset to be modified.
                    PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:lastImageAsset];
                    // Set a property of the request to change the asset itself.
                    request.location = myLocation;

                } completionHandler:^(BOOL success, NSError *error) {
                    NSLog(@"Finished updating asset. %@", (success ? @"Success." : error));
                }];
            }
       }];
    }


    else {
          //if you aren't running ios8 or newer I haven't found a way to add/change metadata but I will keep looking.So if you leave this blank no new metadata is added.
}

您可以添加其他元数据,只需以所需的格式创建数据,然后使用请求。(您想要更改的内容)=新数据。

您可以通过输入类似

的内容来检查数据是否已经存在以及是否存在元数据

lastImageAsset(上面是PHAsset获取结果),然后是属性,如

NSDate * originalFileDate = [lastImageAsset creationDate];等等 有一个清单 https://developer.apple.com/reference/photos/phasset