我的文档文件夹中有一个 gif,我希望该应用程序的用户能够访问并通过电子邮件在其他支持 gif 动画的平台上使用。
我使用这篇文章中的信息生成了 gif 文件...
生成的 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 后从文档目录中删除文件