Objective c PHPhotoLibrary保存gif数据

Objective c PHPhotoLibrary保存gif数据,objective-c,ios9,nsdata,phphotolibrary,Objective C,Ios9,Nsdata,Phphotolibrary,我在新的PHPhotoLibrary中找不到类似于AlassetLibrary->writeImageDataToSavedPhotosAlbum的方法,因为AlassetLibrary在iOS 9中不推荐使用,我无法保存GIF可能我正在使用此代码 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest cre

我在新的PHPhotoLibrary中找不到类似于AlassetLibrary->writeImageDataToSavedPhotosAlbum的方法,因为AlassetLibrary在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
方法
[[phassetCreationRequestCreationRequestforAsset]添加资源类型:PHAssetResourceTypePhoto数据:数据选项:选项]
保存此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);
}];

我已使用照片框架成功地将gif文件保存到照片库

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")
    }
  }

希望这有帮助

你能详细介绍一下你是如何创建临时文件的吗?你应该解释一下为什么你的代码能解决这个问题。请看《谢谢》@陈星旺, 这也是我问题的解决办法。如果你也想发表我的问题,我会接受它作为回答:嗨,我在ios 12上测试了这个,它对我有效,@GeneCode,你导入的
正确吗?或者将“隐私-照片库使用说明”添加到info.plist文件中?