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

时间:2014-08-22 21:16:37

标签: ios save export gif camera-roll

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

我使用此帖子中的信息生成了gif文件...

Create and and export an animated gif via iOS?

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

不幸的是,当尝试使用来自ALAssetsLibrary的UIImageWriteToSavedPhotosAlbum或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创建后从文档目录中删除文件

1 个答案:

答案 0 :(得分:0)

可悲的是,这无法解决。这样做的原因是,照片应用程序无法(目前)显示动画GIF,并且它仅显示来自GIF的一帧作为静态图像。虽然gif没有得到妥善保存,但这并不意味着什么。我还没有尝试过您的代码,但一切似乎都没问题。

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