ios - 将屏幕截图创建的 gif 文件保存到相机胶卷

标签 ios save export gif camera-roll

我的文档文件夹中有一个 gif 我希望该应用程序的用户能够访问和通过电子邮件发送以在支持 gif 动画的其他平台上使用。

我使用这篇文章中的信息生成了 gif 文件...

Create and and export an animated gif via iOS?

gif 文件生成并正确动画(直接从 safari 中模拟器的文档文件夹打开)

不幸的是,当尝试使用 UIImageWriteToSavedPhotosAlbum 或来自 ALAssetsLibrary 的 writeImageDataToSavedPhotosAlbum 将文件移动到相机胶卷(便于用户发送电子邮件)时,图像似乎被转换为 jpg 文件并丢失所有动画。

通过从相机胶卷发送文件并在不同平台上打开(我希望用户拥有的功能)来检查这一点。

我已经阅读了我能找到的每一篇文章,从我所看到的情况来看,似乎可以将 gif 文件直接从浏览器保存到相机胶卷,即使它们在那里没有动画,它们在另一个程序中打开时也会保留该属性所以我希望我正在尝试做的事情至少是可能的:)

感谢您的帮助,在下面包含了我的 gif 创建和失败的复制尝试..

- (void) makeGifFile {


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



///////////////////
NSDictionary *frameProperties = @{
                                  (__bridge id)kCGImagePropertyGIFDictionary: @{
                                          (__bridge id)kCGImagePropertyGIFDelayTime: @0.06f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data
                                          }
                                  };

///////////////////////
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"];

////////////////////////

CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, self.screenshotnumber, NULL);
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);

/////////////////////////////////

for (NSUInteger i = 1; i < self.screenshotnumber+1; i++) {
    @autoreleasepool {

        ////

        NSString *name = [NSString stringWithFormat: @"Screenshot%d", i];
        NSString *pathstring = [NSString stringWithFormat: @"Documents/%@.png", name];


        NSString *gifPath = [NSHomeDirectory() stringByAppendingPathComponent:pathstring];


        /////

        UIImage *image = [UIImage imageWithContentsOfFile:gifPath];

        CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);        }
}


///////////////////////////////////////
if (!CGImageDestinationFinalize(destination)) {
    NSLog(@"failed to finalize image destination");
}
CFRelease(destination);

NSLog(@"url=%@", fileURL);

//////////////////////////////
///
/// saved in documents directory
/////////////////////////////

//////////////////////////
//////
///// now move to camera roll
///////////////////////





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






NSString *documentDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *gifImagePath = [NSString stringWithFormat:@"%@/%@", documentDirectory, @"animated.gif"];
UIImage *gifImage = [UIImage imageWithContentsOfFile:gifImagePath];

UIImageWriteToSavedPhotosAlbum(gifImage, nil, nil, nil);
CCLOG(@"wrote to camera roll");

   //////////////////////// gets saved as JPG not gif



//////// next try...


NSData *data = [NSData dataWithContentsOfFile:gifImagePath]; // Your GIF file path which you might have saved in NSDocumentDir or NSTempDir

[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);

       // success(gifImagePath);
    }
}];


///////////  also gets saved as jpg

}

我为那些感兴趣的人创建屏幕截图的方法...我已经忘记了我发现这个的帖子...如果有人可以向我提供链接,我会在这里给予应有的信任...

包含所有相关功能,以防对其他人有帮助:)

-(UIImage*) screenshotWithStartNode:(CCNode*)startNode
{
[CCDirector sharedDirector].nextDeltaTimeZero = YES;

CGSize viewSize = [[CCDirector sharedDirector] viewSize];
CCRenderTexture* rtx =
[CCRenderTexture renderTextureWithWidth:viewSize.width
                                 height:viewSize.height];
[rtx begin];
[startNode visit];
[rtx end];

return [rtx getUIImage];
}
- (void) saveScreenShotWithName: (NSString*) name
{

CCScene *scene = [[CCDirector sharedDirector] runningScene];
CCNode *n = [scene.children objectAtIndex:0];
UIImage *tempimage = [self screenshotWithStartNode:n];

NSString *pathstring = [NSString stringWithFormat: @"Documents/%@.png", name];

NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:pathstring];

// Write image to PNG

[UIImagePNGRepresentation(tempimage) writeToFile:savePath atomically:YES];


}

简单的循环创建文件,然后另一个循环在 gif 创建后从文档目录中删除文件

最佳答案

很遗憾,这无法解决。这样做的原因是照片应用程序不能(目前)显示动画 GIF,它只能将 GIF 中的一帧显示为静态图像。这并不意味着 gif 没有正确保存。我没有尝试过您的代码,但一切似乎都正常。

有一种方法可以对此进行测试。在消息应用程序中(例如)GIF 正在正确播放,因此如果您通过 ActivityController 从照片应用程序共享 GIF 图像,选择消息,然后将其发送给自己,您应该会在消息应用程序中看到动画 GIF 图像。

关于ios - 将屏幕截图创建的 gif 文件保存到相机胶卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25455647/

相关文章:

iphone - iOS UITableView 错误

ios - 从外部完成 block 检索变量 Parse Swift

ruby - 生成随机字符串并保存到文件

php - 使用 PHP 将 MySQL 数据导出到 CSV 文件显示 1 行和空白 CSV

sql-server - SQL Server 2008中如何写入文本文件

xml - XSL : Avoid exporting namespace definitions to resulting XML documents

ios - 视网膜与常规图像分辨率 : 1 high rez for both or 2 images w/respective rez for each?

ios - 是否可以在 Swift 属性上设置观察点?

c# - WinForm 图表控件 : Change size of chart when saving it to a file

java - 在java中序列化数组列表