iOS_给保存到系统相册的视频添加位置信息

录制结束后的视频要保存到系统相册,之前是不带位置信息的,现在要实现把视频保存到系统相册时添加上位置信息

直接上代码:


#pragma mark - 给视频添加GPS
- (void)saveVideoToSystemAlbumWithPath:(NSString *)videoPath
{

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:videoPath] completionBlock:^(NSURL *assetURL, NSError *error) {
        
        if (error) {
            
            NSLog(@"Save video to system Album failed:%@",error);
        }else{
            
            [self addGPSToSavedVideo];
            
            NSLog(@"Save video to system album success!");
        }
    }];
}

- (void)addGPSToSavedVideo{
    
    //支持ios8以上的版本
    if ([PHAsset class]){
        // 获取相册里所有的视频,并按视频的创建时间排序
        PHFetchOptions *fetchOptions = [[PHFetchOptions alloc]init];
        fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
        fetchOptions.fetchLimit = 1;
        PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:fetchOptions];
        // 拿到最后一个视频资源 即最新的视频资源
        PHAsset *lastAsset = [fetchResult lastObject];
        
        [[PHImageManager defaultManager] requestImageForAsset:lastAsset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:nil resultHandler:^(UIImage *result, NSDictionary *info){
            
            if ([info objectForKey:PHImageErrorKey] == nil && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue]) {
                
                NSArray *resources = [PHAssetResource assetResourcesForAsset:lastAsset];
                NSString *orgFilename = ((PHAssetResource*)resources[0]).originalFilename;
                //我们一旦拿到asset,就可以看到metadata信息
                [lastAsset requestContentEditingInputWithOptions:nil completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
                    NSURL *imageURL = contentEditingInput.fullSizeImageURL;
                    NSString *urlstring = [imageURL absoluteString];
                    NSLog(@"Urlstring = %@",urlstring);
                }];
                
                //用坐标和日期创建新的location
                double latitude = [TVULocalManager getLatitude];
                double longitude = [TVULocalManager getLongitude];
                CLLocationCoordinate2D locationNew = CLLocationCoordinate2DMake(latitude,longitude);
                NSDate *nowDate = [NSDate date];
                CLLocation *newLocation = [[CLLocation alloc ]initWithCoordinate:locationNew altitude:0.0 horizontalAccuracy:1.0 verticalAccuracy:1.0 timestamp:nowDate];
                
                //我们请求更改metadata,并插入新的location
                //当视频已经被保存的回调被触发时,将会把写的元数据写入视频
                 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                    // 从要被修改元数据的Asset中创建修改请求
                    PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:lastAsset];
                    // 设置请求的属性来改变Asset
                    request.location = newLocation;
                    
                } completionHandler:^(BOOL success, NSError *error) {
                    
                    NSLog(@"Finished updating asset. %@", (success ? @"Success." : error));
                }];
            }
        }];
        
        
    }else {
        //如果是iOS8之前的版本  暂时不支持添加位置信息
        NSLog(@"Pre-iOS8 does not support adding GPS");
    }
}
zhu
【注】要记得添加头文件

#import

#import


你可能感兴趣的:(iOS)