Save generated GIF to camera roll?

后端 未结 4 2242
一个人的身影
一个人的身影 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条回答
  •  别跟我提以往
    2021-02-20 19:05

    **

    Solution 01 : Only saving the existing GIF file to Camera Roll

    **

    As I understand your problem. You are able to generate a GIF file but cannot save and also view it to the Camera Roll.

    So I am attaching a sample test using existing GIF File.

    Step 01. I copied a gif IMG_0009.GIF file in my Application Document directory.

    Step 02 Than I use the below code to load this files NSData:

    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
    NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"IMG_0009.gif"];
    
    NSData *gifData = [NSData dataWithContentsOfFile:[fileURL path]];
    

    Step 03: Now I save the file in the Media Directory:

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeImageDataToSavedPhotosAlbum:gifData metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
    
        NSLog(@"Success at %@", [assetURL path] );
    }];
    

    The Asset URL is proper. Now you can check you media directory. you can locate the saved gif image.

    Have Fun :)


    **

    Solution 02: Demo of Creating and saving GIF to Camera roll

    **

    I cloned some solution to show creating and saving of GIF files to Camera Roll. You can download and check my fork at github:

    The demo creates a GIF file by taking 2 or more images and save in the Camera Roll Directory

    https://github.com/bllakjakk/Giraffe

    The main Code to focus is like below:

    [export encodeToFile:tempFile callback:^(NSString * aFile) {
    
        NSLog(@"Path: %@", aFile);
    
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    
        NSData *data = [NSData dataWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:aFile]];
    
        [library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
    
            NSLog(@"Success at %@", [assetURL path] );
        }];
    

    }];

    It uses the library as I mentioned in my solution before http://jitsik.com/wordpress/?p=208

    How to verify:

    Step 01: Run the demo project.

    Step 02: As directed by the application add 2 images and click Export.

    Step 03: Now check the camera roll you will find the created gif.

    Previous:

    GIF is a proprietary format, so you would need a 3rd party lib to save it.

    check following link: http://jitsik.com/wordpress/?p=208

提交回复
热议问题