Save generated GIF to camera roll?

后端 未结 4 1978
一个人的身影
一个人的身影 2021-02-20 18:39

Thanks for reading. I\'ve created a GIF using methods from this question:

Create and and export an animated gif via iOS?

I\'m trying to use the only method that

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-20 19:02

    Here is an updated answer using PHPhotoLibrary, since ALAssetsLibrary is deprecated.

    I used this answer from another user - 陈星旺

    PHPhotoLibrary save a gif data

    NSString *exportPath = [NSTemporaryDirectory() stringByAppendingString:@"/animated.gif"];
    NSURL *fileURL = [NSURL fileURLWithPath:exportPath isDirectory:NO];
    NSData * gifData = [NSData dataWithContentsOfFile:fileURL.absoluteString];
    
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    
        PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
        [[PHAssetCreationRequest creationRequestForAsset]
         addResourceWithType:PHAssetResourceTypePhoto
         data:gifData
         options:options];
    
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
    
        if (success) {
            NSLog(@"image saved!");
        } else {
            NSLog(@"error saving image - %@", error ? error.localizedDescription : @"");
        }
    }];
    

    If you needed to download the GIF data from a URL, you could use this:

    NSData *gifData = [NSData dataWithContentsOfURL:theGIFsURL];
    

提交回复
热议问题