3

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);
        }
    }];
Bader
  • 825
  • 1
  • 9
  • 26

3 Answers3

8

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);
}];
NewShelbyWoo
  • 722
  • 1
  • 7
  • 21
陈星旺
  • 126
  • 1
  • 5
  • 1
    you should give an explanation why your code solves the problem. Please look at [answer] – JimHawkins Nov 02 '16 at 06:42
  • Thanks @陈星旺, this was the solution to my issue also. If you want to post on my question as well, I'll accept it as an answer: http://stackoverflow.com/questions/40370773/how-to-save-gifs-with-the-phphotolibrary – NewShelbyWoo Nov 02 '16 at 19:34
  • Hi , I test this at ios 12, it works for me, @GeneCode, did you import `` correctly ? or add "Privacy - Photo Library Usage Description" to your info.plist file ? – 陈星旺 Oct 30 '18 at 07:37
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!

Tuan Do
  • 283
  • 1
  • 9
1

I solved my issue by creating temp file and using :

creationRequestForAssetFromImageAtFileURL

Bader
  • 825
  • 1
  • 9
  • 26