PHPhotoLibrary save a gif data

☆樱花仙子☆ 提交于 2019-12-30 07:19:30

问题


I can't find a similar method to ALAssetsLibrary->writeImageDataToSavedPhotosAlbum in the new PHPhotoLibrary since ALAssetsLibrary deprecated in iOS 9 I can't save GIF probably I'm using this code

[[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);
        }
    }];

回答1:


I used NSData to save the gif image, ALAssetsLibrary method UIImageWriteToSavedPhotosAlbum deprecated after iOS8,The api says we can use PHAssetCreationRequestmethod [[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:data options:options]; to save this gif image data, so you can use url request to save gifs as well

codes are follows:

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);
}];



回答2:


I've save gif file successfully to Photos Gallery by using Photos Framework

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

Hope this help!




回答3:


I solved my issue by creating temp file and using :

creationRequestForAssetFromImageAtFileURL



来源:https://stackoverflow.com/questions/35374919/phphotolibrary-save-a-gif-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!