When ios saves the video to the album, modify the creation time attribute of the video

question

I encountered a problem in the process of downloading a video to the sandbox according to the link, and then saving it to the photo album:

When I save the video file saved in the sandbox to the album, the time of the video displayed in the album is not the time when I downloaded the video, but the creationDate attribute inside the video

Solution

  //#import <Photos/Photos.h>


  // 此路径为保存到沙盒中的视频文件的完整路径
  NSString *fullFilePath = "/Users/username/Library/Developer/CoreSimulator/Devices/88B0FE76-575F-41A9-BF5F-135DA452241D/data/Containers/Data/Application/8FEDE3D6-0B5B-420F-9A6E-7907F8AF8188/Library/Caches/FjYwxHtjg9-z8LLD-sEmIP_VfCow.mp4";

  // 保存视频到相册,同时修改视频的创建时间属性
  PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
  [photoLibrary performChanges:^{
    // 将视频保存到相册中
    PHAssetChangeRequest *request = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:fullFilePath]];
    // 修改视频的创建时间属性
    request.creationDate = [NSDate date];
  } completionHandler:^(BOOL success, NSError * _Nullable error) {
    if (success) {
      NSLog(@"已将视频保存至相册");
    } else {
      NSLog(@"未能保存视频到相册");
    }
  }];

limitations

Because of the introduction of Photos Framework, it only supports iOS8.0 and above

reference

Photos(PHAssetChangeRequest)

Guess you like

Origin blog.csdn.net/weixin_41786574/article/details/109226283