GIF在计算机上动画太快 - iOS

时间:2014-06-10 11:47:36

标签: ios gif animated-gif

我正在尝试创建GIF动画图像,我传递了一系列图像。

假设我有一个4秒的视频,我从中提取了大约120帧。无论创建的GIF大小如何,我都会从所有120帧中创建一个GIF。问题是,当我在iPhone中打开GIF(通过将其附加到MailViewComposer或iMessage)时,它运行正常,但如果我通过电子邮件发送或将其导入计算机,它运行得太快。谁能提出这里有什么问题?

我正在使用HJImagesToGIF进行GIF创建。 GIF属性的字典如下:

NSDictionary *prep = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.03f]
                                                                                    forKey:(NSString *) kCGImagePropertyGIFDelayTime]
                                                 forKey:(NSString *) kCGImagePropertyGIFDictionary];

NSDictionary *fileProperties = @{
                                 (__bridge id)kCGImagePropertyGIFDictionary: @{
                                         (__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
                                         }
                                 };

创建GIF:

CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path];

CGImageDestinationRef dst = CGImageDestinationCreateWithURL(url, kUTTypeGIF, [images count], nil);
CGImageDestinationSetProperties(dst, (__bridge CFDictionaryRef)fileProperties);

for (int i=0;i<[images count];i++)
{
    //load anImage from array
    UIImage * anImage = [images objectAtIndex:i];

    CGImageDestinationAddImage(dst, anImage.CGImage,(__bridge CFDictionaryRef)(prep));

}

bool fileSave = CGImageDestinationFinalize(dst);
CFRelease(dst);
if(fileSave) {
    NSLog(@"animated GIF file created at %@", path);
}else{
    NSLog(@"error: no animated GIF file created at %@", path);
}

要保存GIF,我正在使用:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:tempPath]];
data = [NSData dataWithContentsOfFile:tempPath];

[library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
    if (error) {
        NSLog(@"Error Saving GIF to Photo Album: %@", error);
    } else {
        // TODO: success handling
        NSLog(@"GIF Saved to %@", assetURL);
    }
}];

谢谢大家。

0 个答案:

没有答案