PHPhotoLibrary 保存一个 gif 数据

PHPhotoLibrary save a gif data

我在新的 PHPhotoLibrary 中找不到与 ALAssetsLibrary->writeImageDataToSavedPhotosAlbum 类似的方法,因为 ALAssetsLibrary 在 iOS 中已弃用 9 我无法保存 GIF 可能我正在使用此代码

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[UIImage imageWithData:gifdata]];
        placeholder = [assetRequest placeholderForCreatedAsset];
        photosAsset = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
        PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection
                                                                                                                      assets:photosAsset];
        [albumChangeRequest addAssets:@[placeholder]];
    } completionHandler:^(BOOL success, NSError *error) {
        if (success)
        {

        }
        else
        {

            NSLog(@"%@", error);
        }
    }];

我通过创建临时文件并使用解决了我的问题:

creationRequestForAssetFromImageAtFileURL

我使用NSData保存gif图片,ALAssetsLibrary方法UIImageWriteToSavedPhotosAlbum在iOS8之后被弃用,api说我们可以使用 PHAssetCreationRequest方法[[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:data options:options];保存这个gif图像数据,所以你也可以使用url请求来保存gif

代码如下:

NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
    [[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:data options:options];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
    NSLog(@":%d",success);
}];

我已经使用 Photos Framework 将 gif 文件成功保存到 Photos Gallery

PHPhotoLibrary.shared().performChanges ({
    let url = URL(fileURLWithPath: "your file path")
    PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: url)
  }) { saved, error in
    if saved {
      print("Your image was successfully saved")
    }
  }

希望对您有所帮助!