iOS 导入相册视频文件

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = NO;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePickerController.mediaTypes =[NSArray arrayWithObjects: (NSString *)kUTTypeMovie, nil];
    //imagePickerController.videoQuality = UIImagePickerControllerQualityType640x480;
    imagePickerController.videoQuality=UIImagePickerControllerQualityTypeMedium;
    
    [self presentViewController:imagePickerController animated:YES completion:nil];


#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeMovie]) {
        
        NSString *videoName = [dateFormatter2 stringFromDate:[NSDate date]];
        _importVideoPath = [NSHomeDirectory() stringByAppendingPathComponent:[_videoPath stringByAppendingString:
                            [NSString stringWithFormat:@"%@.mov",videoName]]];
        NSLog(@"_importVideoPath:%@",_importVideoPath);
        NSError *error;
        NSData *videoData = [NSData dataWithContentsOfURL:[info objectForKey:UIImagePickerControllerMediaURL]];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL success = [fileManager fileExistsAtPath:_importVideoPath];
        
        if(success) {
            NSLog(@"found a video and removeItemAtPath");
            success = [fileManager removeItemAtPath:_importVideoPath error:&error];
        }
        BOOL writeToFile=[videoData writeToFile:_importVideoPath atomically:NO];
        NSLog(@"导入成功 writeToFile:%@",writeToFile==YES?@"YES":@"NO");
        
        [picker dismissViewControllerAnimated:YES completion:^{
            //将视频信息插入数据库
            [self getVideoInfo:_importVideoPath];
            long long size=[DiskSpaceTool fileSizeAtPath:_importVideoPath];
            //插入数据库
            [dbManager insertWithTable:kLocalVideosTableName value_map:
             @{kLocalVideos_Name:[NSString stringWithFormat:@"%@.mov",videoName],
               kLocalVideos_OriginPath:_importVideoPath,
               kLocalVideos_LengthString:_lengthString,
               kLocalVideos_Length:[NSString stringWithFormat:@"%d",_length],
               kLocalVideos_Size:[NSString stringWithFormat:@"%lld",size],
               kLocalVideos_CoverPath:@"",
               kLocalVideos_CoverTime:@"0",
               kLocalVideos_CreateDate:videoName,
               kLocalVideos_UploadSeccuss:@"0"}];
            
            //刷新
            [self refreshTableView];

        }];
    }
    
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


你可能感兴趣的:(IOS)