NSRare / NSGIF

🔮 iOS Library for converting videos to animated GIFs.
MIT License
923 stars 190 forks source link

GIF not recognised in other app. #21

Closed sagarbhatt4 closed 7 years ago

sagarbhatt4 commented 7 years ago

Once GIF is created and stored in iPhone Photos, Its not getting recognised as GIF for other apps. e.g in Twitter app, Saved GIF in Photos not getting recognised as gif. Other GIF which are not created using NSGIF and stored in Photos are recognised.

sebyddd commented 7 years ago

The GIFs generated using NSGIF are stored in the Documents folder of the app. Can you please share the way you are saving it to the camera roll?

sagarbhatt4 commented 7 years ago

Thanks for your reply. I fixed it.

In function.

Below line CGImageDestinationSetProperties(destination, (CFDictionaryRef)fileProperties); is after CGImageDestinationAddImage(destination, imageRef, (CFDictionaryRef)frameProperties); But its need to be before FOR loop It will be look like this after change

+ (NSURL )createGIFforTimePoints:(NSArray )timePoints fromURL:(NSURL )url fileProperties:(NSDictionary )fileProperties frameProperties:(NSDictionary *)frameProperties frameCount:(int)frameCount gifSize:(GIFSize)gifSize{

    CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF , frameCount, NULL);   

    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];

    AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
    generator.appliesPreferredTrackTransform = YES;

    CMTime tol = CMTimeMakeWithSeconds([tolerance floatValue], [timeInterval intValue]);
    generator.requestedTimeToleranceBefore = tol;
    generator.requestedTimeToleranceAfter = tol;

    NSError *error = nil;
    CGImageRef previousImageRefCopy = nil;
    int count = 1;
    CGImageDestinationSetProperties(destination, (CFDictionaryRef)fileProperties);
    for (NSValue *time in timePoints) {
        CGImageRef imageRef;

#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
        imageRef = (float)gifSize/10 != 1 ? createImageWithScale([generator copyCGImageAtTime:[time CMTimeValue] actualTime:nil error:&error], (float)gifSize/10) : [generator copyCGImageAtTime:[time CMTimeValue] actualTime:nil error:&error];
#elif TARGET_OS_MAC
        imageRef = [generator copyCGImageAtTime:[time CMTimeValue] actualTime:nil error:&error];
#endif
        if (error) {
            //NSLog(@"Error copying image: %@", error);
        }
        if (imageRef) {
            CGImageRelease(previousImageRefCopy);
            previousImageRefCopy = CGImageCreateCopy(imageRef);
        } else if (previousImageRefCopy) {
            imageRef = CGImageCreateCopy(previousImageRefCopy);
        } else {
            NSLog(@"Error copying image and no previous frames to duplicate");
            return nil;
        }
        CGImageDestinationAddImage(destination, imageRef, (CFDictionaryRef)frameProperties);

        CGImageRelease(imageRef);
    }
    CGImageRelease(previousImageRefCopy);

    // Finalize the GIF
    if (!CGImageDestinationFinalize(destination)) {
        NSLog(@"Failed to finalize GIF destination: %@", error);
        if (destination != nil) {
            CFRelease(destination);
        }
        return nil;
    }
    CFRelease(destination);    
    return fileURL;
}